[英]Why I got an incomplete type error and how to solve it?
This is a code a got for now but it doesnt compile due to an incomplete variable type container::iterator error. 这是目前获得的代码,但由于变量类型的container :: iterator错误,所以无法编译。 The class and the iterator are still pretty simple since I am trying to figure out how to organise my code.
由于我试图弄清楚如何组织代码,因此类和迭代器仍然非常简单。
template <typename T> class container {
using size_type = std::size_t;
using pointer = T*;
using const_pointer = const T*;
public:
class iterator;
container (size_type n=0): data {n>0 ? new T[n]:nullptr}, size {n>0 ? n:0}{};
container (iterator begin, iterator end){};
~container (){ delete[] data; };
private:
pointer data;
size_type size;
};
template <typename T> class container<T>::iterator {
public:
iterator (pointer p): current {p} {};
iterator& operator++ (){ current++; return *this; };
iterator& operator-- (){ current--; return *this; };
reference operator* (){ return *current; };
bool operator== (const iterator& b) const { return current == b.current; };
bool operator!= (const iterator& b) const { return current != b.current; };
private:
pointer current;
};
I would like to keep the iterator definition out of the container class if possible. 如果可能的话,我想将迭代器定义排除在容器类之外。 Thanks for any replies.
感谢您的任何答复。
I would like to keep the iterator definition out of the container class if possible.
如果可能的话,我想将迭代器定义排除在容器类之外。
It isn't, because the arguments for container
's member functions cannot be properly analysed without a definition for their type. 并非如此,因为没有定义
container
就无法正确分析container
成员函数的参数。
Move it inline. 内联移动。
container (iterator begin, iterator end){};
That uses iterator
but at that point in the code there's no definition of iterator
, only a declaration. 它使用了
iterator
但是在代码中没有iterator
的定义,只有一个声明。 You'll need to define the structure of your iterator before you can even declare this constructor. 您需要定义迭代器的结构,然后才能声明此构造函数。
You could alter this constructor to take references. 您可以更改此构造函数以获取引用。 Then you can declare it within your class but then define it below the definition for iterator.
然后,您可以在您的类中声明它,然后在迭代器的定义下定义它。 IMO this isn't the best way to go, but it's possible.
IMO这不是最好的方法,但是有可能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.