- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于我正在从事的项目,我创建了一个封装数据结构的 C++ 库。对于每个数据结构,我都创建了自定义迭代器来优雅地浏览数据。一切都很顺利,直到我在这些类上尝试了模板特化,这时我偶然发现了这个构建错误:错误:不完整类型的无效使用。
我已经为此苦苦思索了一个多星期,但在网上找不到任何有用的东西,所以我想也许你们中的一些人可以帮助我解决这个问题......
整个问题简化了:
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 { ... }
例子:
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;
};
构建错误:
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;
最佳答案
错误信息很清楚。
class DoublyLinkedListIterator<double>
当您尝试在以下函数中创建类的实例时,它是一个不完整的类型:
iterator begin() {
return iterator(this);
}
iterator end(){
return iterator(count);
}
您可以使用以下任一方法解决此问题。
移动template<typename T> class DoublyLinkedListIterator
的定义在 template<> class DataStructure<double>
的定义之前.
不要内联定义上述函数。只声明它们。在 template<typename T> class DoublyLinkedListIterator
的定义之后定义它们.
关于C++ 模板特化友元迭代器错误 : invalid use of incomplete type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38279152/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
正如您在 this travis.yml 中看到的那样文件,我的代码依赖于一些第三方库,我在构建项目之前将它们安装在远程系统上。 Travis 每次推送提交时都会下载并构建这些库,这可以避免吗?我的意
我是一名优秀的程序员,十分优秀!