[英]expected constructor, destructor, or type conversion before ‘*’ token
[英]Templates, nested classes, and “expected constructor, destructor, or conversion before '&' token”
在使用一些模板並使用迭代器編寫自己的基本容器類時,我發現自己需要將成員函數體從模板類移動到單獨的文件中以符合樣式指南。 但是,我遇到了一個有趣的編譯錯誤:
runtimearray.cpp:17:錯誤:'&'令牌之前的構造函數,析構函數或類型轉換runtimearray.cpp:24:錯誤:'&'令牌之前的構造函數,析構函數或類型轉換runtimearray.cpp:32:錯誤: '&'標記之前的預期構造函數,析構函數或類型轉換runtimearray.cpp:39:錯誤:'&'標記之前的構造函數,析構函數或類型轉換runtimearray.cpp:85:錯誤:預期的構造函數,析構函數或類型轉換在“RuntimeArray”之前runtimearray.cpp:91:錯誤:在'RuntimeArray'之前的預期構造函數,析構函數或類型轉換
runtimearray.h:
#ifndef RUNTIMEARRAY_H_
#define RUNTIMEARRAY_H_
template<typename T>
class RuntimeArray
{
public:
class Iterator
{
friend class RuntimeArray;
public:
Iterator(const Iterator& other);
T& operator*();
Iterator& operator++();
Iterator& operator++(int);
Iterator& operator--();
Iterator& operator--(int);
bool operator==(Iterator other);
bool operator!=(Iterator other);
private:
Iterator(T* location);
T* value_;
};
RuntimeArray(int size);
~RuntimeArray();
T& operator[](int index);
Iterator Begin();
Iterator End();
private:
int size_;
T* contents_;
};
#endif // RUNTIMEARRAY_H_
runtimearray.cpp:
#include "runtimearray.h"
template<typename T>
RuntimeArray<T>::Iterator::Iterator(const Iterator& other)
: value_(other.value_)
{
}
template<typename T>
T& RuntimeArray<T>::Iterator::operator*()
{
return *value_;
}
template<typename T>
RuntimeArray<T>::Iterator& RuntimeArray<T>::Iterator::operator++()
{
++value_;
return *this;
}
template<typename T>
RuntimeArray<T>::Iterator& RuntimeArray<T>::Iterator::operator++(int)
{
Iterator old = *this;
++value_;
return old;
}
template<typename T>
RuntimeArray<T>::Iterator& RuntimeArray<T>::Iterator::operator--()
{
--value_;
return *this;
}
template<typename T>
RuntimeArray<T>::Iterator& RuntimeArray<T>::Iterator::operator--(int)
{
Iterator old = *this;
--value_;
return old;
}
template<typename T>
bool RuntimeArray<T>::Iterator::operator==(Iterator other)
{
return value_ == other.value_;
}
template<typename T>
bool RuntimeArray<T>::Iterator::operator!=(Iterator other)
{
return value_ != other.value_;
}
template<typename T>
RuntimeArray<T>::Iterator::Iterator(T* location)
: value_(location)
{
}
template<typename T>
RuntimeArray<T>::RuntimeArray(int size)
: size_(size),
contents_(new T[size])
{
}
template<typename T>
RuntimeArray<T>::~RuntimeArray()
{
if(contents_)
delete[] contents_;
}
template<typename T>
T& RuntimeArray<T>::operator[](int index)
{
return contents_[index];
}
template<typename T>
RuntimeArray<T>::Iterator RuntimeArray<T>::Begin()
{
return Iterator(contents_);
}
template<typename T>
RuntimeArray<T>::Iterator RuntimeArray<T>::End()
{
return Iterator(contents_ + size_);
}
如何讓這些錯誤消失? 這些文件對我來說很有意義,但唉,編譯器認為這很重要。
我認為你缺少typename
關鍵字。
例如
template<typename T>
RuntimeArray<T>::Iterator& RuntimeArray<T>::Iterator::operator++()
應該
template<typename T>
typename RuntimeArray<T>::Iterator& RuntimeArray<T>::Iterator::operator++()
依賴於模板參數的“嵌套”類型需要使用typename
關鍵字來告訴編譯器它們應該是類型,否則這些類型將是不明確的。
這是一個有趣的風格指南。 一般情況下,模板函數的定義必須在頭文件。 這是在幾個小時前發生的:將模板化的C ++類拆分為.hpp / .cpp文件 - 是否可能?
這不會按照你想要的方式工作。 所有函數聲明和定義必須出現在您定義RuntimeArray的.h文件中。 你看到的錯誤可能是其他東西,也許是一個typename的東西,但即使你可以讓RunTimeArray.cpp單獨編譯,也沒有人能夠使用它。
如果你真的必須在一個單獨的文件中定義,那么#include
它在runtimearray.h的末尾
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.