以前はイテレータで作業したことがありません。私が書いたカスタムコンテナクラスのカスタムイテレータを設計するのに問題があります。C++ Iterators戻り値の型
背景:
class RandomArray
{
friend ostream& operator<<(ostream&, const RandomArray&);
public:
class iterator
{
public:
typedef iterator self_type;
typedef int* pointer;
typedef int& reference;
self_type operator++() { self_type i = *this; ptr++; return i;}
reference operator*() {return *ptr;}
bool operator!=(const self_type& rhs) {return ptr != rhs.ptr;}
private:
pointer ptr;
};
class const_iterator
{
public:
typedef const_iterator self_type;
typedef int* pointer;
typedef int& reference;
self_type operator++() { self_type i = *this; ptr++; return i;}
const reference operator*() { return *ptr; }
bool operator!=(const self_type& rhs) {return ptr != rhs.ptr;}
private:
pointer ptr;
};
RandomArray();
RandomArray(size_t);
size_t size() const;
int* begin();
iterator begin();
const int* begin() const;
const iterator begin() const;
int* end();
iterator end();
const int* end() const;
const iterator end() const;
private:
size_t capacity;
int* data;
};
私は「エラー:
TEST(RandomArray, End) {
RandomArray r(17);
int *b = r.begin();
int *e = r.end();
EXPECT_EQ(b + 17, e);
}
TEST(RandomArray, IteratorTypedef) {
RandomArray r(7);
for (RandomArray::iterator it = r.begin(); it != r.end(); ++it) {
*it = 89;
EXPECT_EQ(89, *it);
}
}
はここに私のヘッダファイルとイテレータのコードです:これらは私が持っている2つのテストケースをされている、GoogleがAPIをテスト使用最初と最後になるのは次のようなものです:Error: Cannot overload functions distinguished by return type alone.
同じ関数名と同じパラメータを異なる戻り値の型で使用することはできませんこれを行うより良い方法があるのだろうか?イテレーターを正しく作っていますか?テンプレートはこれを解決するのに役立つでしょうか? int*
とiterator
の両方を返すには、begin()
とend()
が必要です。両方のテストケースを渡すことができます。これを達成するより良い方法はありますか?
は、ポインタリターンを取り除くだけイテレータを返しなさい!
は、コードを更新しましたか – NathanOliver
テストケースを修正してください。彼らが求めているものは不可能です。 – ubik