[英]C++ template specialisation friend iterator error: invalid use of incomplete type
For a project I'm working at, I've created a C++ library that encapsulates data structures. 对于我正在从事的项目,我创建了一个封装数据结构的C ++库。 For each data structure, I've created custom iterators to navigate through data elegantly. 对于每种数据结构,我都创建了自定义迭代器,以优雅地浏览数据。 Everything went fine until I've tried template specialization on those classes, when I stumbled upon this build error: error: invalid use of incomplete type. 当我偶然发现以下构建错误时,一切都进行得很好,直到我尝试对这些类进行模板专业化: 错误:无效使用不完整类型。
I've banged my head for more than a week on this and found nothing helpful on the net, so I thought maybe some of you can help me with this... 我已经为此花了一个多星期不停地敲打头球,发现网上没有任何帮助,所以我想也许有些人可以帮助我...
The whole problem simplified: 整个问题简化了:
template<typename T>
class DataStructureIterator;
// OK
template<typename T>
class DataStructure {
friend class DataStructureIterator<T>;
public:
typedef DataStructureIterator<T> iterator;
...
iterator begin() {
return iterator(this);
}
iterator end(){
return iterator(count);
}
};
// ERROR: "Invalid use of incomplete type" (on ANY specialization attempt)
template<>
class DataStructure<double> {
friend class DataStructureIterator<double>;
public:
typedef DataStructureIterator<double> iterator;
...
iterator begin() {
return iterator(this);
}
iterator end(){
return iterator(count);
}
};
template<typename T>
class DataStructureIterator { ... }
Example: 例:
template<typename T>
struct DoublyLinkedListEntry {
T value;
DoublyLinkedListEntry* next;
DoublyLinkedListEntry* previous;
};
template<typename T>
class DoublyLinkedListIterator;
template<typename T>
class DoublyLinkedList {
friend class DoublyLinkedListIterator<T>;
public:
typedef DoublyLinkedListIterator<T> iterator;
DoublyLinkedList() {
head = nullptr;
tail = nullptr;
count = 0;
}
~DoublyLinkedList() {
empty();
}
void add(const T& value) {
DoublyLinkedListEntry<T>* element = new DoublyLinkedListEntry<T>;
element->value = value;
element->next = nullptr;
element->previous = tail;
if(head==nullptr) {
head = element;
} else {
tail->next = element;
}
tail = element;
++ count;
}
iterator begin() {
return iterator(this);
}
iterator end(){
return iterator(count);
}
private:
void empty(){
DoublyLinkedListEntry<T>* temp = head;
DoublyLinkedListEntry<T>* del = temp;
while(del != nullptr) {
temp = temp->next;
delete del;
del = temp;
}
}
DoublyLinkedListEntry<T>* head;
DoublyLinkedListEntry<T>* tail;
std::size_t count;
};
template<>
class DoublyLinkedList<double> {
friend class DoublyLinkedListIterator<double>;
public:
typedef DoublyLinkedListIterator<double> iterator;
DoublyLinkedList() {
head = nullptr;
tail = nullptr;
count = 0;
}
~DoublyLinkedList() {
empty();
}
void add(double& value) {
DoublyLinkedListEntry<double>* element = new DoublyLinkedListEntry<double>;
element->value = value;
element->next = nullptr;
element->previous = tail;
if(head==nullptr) {
head = element;
} else {
tail->next = element;
}
tail = element;
++ count;
}
iterator begin() {
return iterator(this);
}
iterator end(){
return iterator(count);
}
private:
void empty(){
DoublyLinkedListEntry<double>* temp = head;
DoublyLinkedListEntry<double>* del = temp;
while(del != nullptr) {
temp = temp->next;
delete del;
del = temp;
}
}
DoublyLinkedListEntry<double>* head;
DoublyLinkedListEntry<double>* tail;
std::size_t count;
};
template<typename T>
class DoublyLinkedListIterator {
public:
DoublyLinkedListIterator(){
list = nullptr;
current_item = nullptr;
offset = 0;
}
DoublyLinkedListIterator(DoublyLinkedList<T>* list){
this->list = list;
current_item = list->head;
offset = 0;
}
DoublyLinkedListIterator(std::size_t total){
list = nullptr;
current_item = nullptr;
offset = total;
}
~DoublyLinkedListIterator(){}
const T operator*(){
return current_item->value;
}
bool operator!=(const DoublyLinkedListIterator<T>& it) const {
return offset!=it.offset;
}
DoublyLinkedListIterator<T>& operator++(){
if(current_item!=nullptr) {
current_item = current_item->next;
}
++offset;
return *this;
}
private:
DoublyLinkedList<T>* list;
DoublyLinkedListEntry<T>* current_item;
std::size_t offset;
};
Build error: 生成错误:
In file included from ../src/Learning.cpp:11:0:
../src/DoublyLinkedList.h: In member function ‘DoublyLinkedList<double>::iterator DoublyLinkedList<double>::begin()’:
../src/DoublyLinkedList.h:107:20: error: return type ‘DoublyLinkedList<double>::iterator {aka class DoublyLinkedListIterator<double>}’ is incomplete
iterator begin() {
^
../src/DoublyLinkedList.h:108:24: error: invalid use of incomplete type ‘DoublyLinkedList<double>::iterator {aka class DoublyLinkedListIterator<double>}’
return iterator(this);
^
../src/DoublyLinkedList.h:22:7: error: declaration of ‘DoublyLinkedList<double>::iterator {aka class DoublyLinkedListIterator<double>}’
class DoublyLinkedListIterator;
^
../src/DoublyLinkedList.h: In member function ‘DoublyLinkedList<double>::iterator DoublyLinkedList<double>::end()’:
../src/DoublyLinkedList.h:111:17: error: return type ‘DoublyLinkedList<double>::iterator {aka class DoublyLinkedListIterator<double>}’ is incomplete
iterator end(){
^
../src/DoublyLinkedList.h:112:25: error: invalid use of incomplete type ‘DoublyLinkedList<double>::iterator {aka class DoublyLinkedListIterator<double>}’
return iterator(count);
^
../src/DoublyLinkedList.h:22:7: error: declaration of ‘DoublyLinkedList<double>::iterator {aka class DoublyLinkedListIterator<double>}’
class DoublyLinkedListIterator;
The error message is pretty clear. 错误消息很清楚。
class DoublyLinkedListIterator<double>
is an incomplete type when you try to create instances of the class in the following functions: 当您尝试在以下函数中创建类的实例时, class DoublyLinkedListIterator<double>
是不完整的类型:
iterator begin() {
return iterator(this);
}
iterator end(){
return iterator(count);
}
You can resolve this using either of the following methods. 您可以使用以下两种方法之一解决此问题。
Move the definition of template<typename T> class DoublyLinkedListIterator
before the definition of template<> class DataStructure<double>
. 将template<typename T> class DoublyLinkedListIterator
的定义template<typename T> class DoublyLinkedListIterator
template<> class DataStructure<double>
的定义之前。
Don't define the above functions inline. 不要内联定义以上函数。 Only declare them. 只声明它们。 Define them after the definition of template<typename T> class DoublyLinkedListIterator
. 在template<typename T> class DoublyLinkedListIterator
的定义之后定义它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.