简体   繁体   English

为什么我收到不完整的类型错误以及如何解决?

[英]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.

相关问题 我该如何解决C ++中的不完整类型错误? - How can i solve the incomplete type error in c++? 为什么此代码不起作用? 我收到一个错误:类型不完整或未命名类型 - Why doesn't this code work? I got an error: incomplete type or does not name a type 如何解决“[错误]无效使用不完整类型&#39;类SLLNode&#39;”链接列表 - How do I solve “[Error] invalid use of incomplete type 'class SLLNode'” Linked Lists 如何解决字段“类名”具有不完整的类型错误 - How to solve field 'classname' has incomplete type error 我怎么知道是什么原因导致错误的不完整类型不完整:对指向不完整类型的指针进行算术运算 - How can I know what makes an incomplete type incomplete for error: arithmetic on a pointer to an incomplete type 我收到错误信息:“变量的类型不完整,类型为” void”,但我找不到问题所在 - I got the error 'variable has incomplete type 'void'', and I can't find what's wrong 为什么这个构造函数没有给出不完整的类型错误? - Why is this constructor not giving an incomplete type error? 使用不完整类型无效 - 为什么在这种情况下没有错误? - Invalid use of incomplete type - why no error in this case? 为什么编译器在此代码中说“不完整类型错误”? - why compiler is saying Incomplete type error in this code? 为什么我得到一个“变量&#39;std :: packaged_task <int> 任务&#39;具有初始化程序,但类型不完整”错误 - Why do I get an “variable 'std::packaged_task<int> task' has initializer but incomplete type” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM