我有两个模板类,第一个是:
template <typename T>
class LL_iterator
{...}; 以及:
template <class T>
class LL
{...}现在,问题是我试图在具有'LL_iterator‘返回类型的'LL’类中编写一个函数声明。'LL‘类中的函数如下所示:
LL_iterator<T> begin();我所收到的错误如下:
Error 1 error C2143: syntax error : missing ';' before '<' c:\users\vismark1994\documents\visual studio 2013\projects\project 4\project 4\ll.h 103 1 Project 4
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\vismark1994\documents\visual studio 2013\projects\project 4\project 4\ll.h 103 1 Project 4
Error 3 error C2238: unexpected token(s) preceding ';' c:\users\vismark1994\documents\visual studio 2013\projects\project 4\project 4\ll.h 103 1 Project 4
Error 4 error C1903: unable to recover from previous error(s); stopping compilation c:\users\vismark1994\documents\visual studio 2013\projects\project 4\project 4\ll.h 103 1 Project 4以下是完整的类声明:
template <class T>
class LL
{
private:
int count;
Node<T>* head;
Node<T>* tail;
void copyList(const LL<T> &listToCopy);
public:
LL();
LL(const LL<T> &otherLL);
~LL();
void push_back(T);
void push_front(T);
void pop_back();
void clear();
int size() const;
T& at(int ndx);
T& operator[] (int ndx);
T& at(int ndx) const;
T& operator[] (int ndx) const;
const LL& operator=(const LL<T> & rhsObj);
LL_iterator<T> begin(); // <-- this is where problem is
};
template <typename T>
class LL_iterator
{
private:
Node<T> *current;
public:
LL_iterator();
LL_iterator(Node<T> *ptr);
T& operator*();
LL_iterator operator++();
bool operator==(const LL_iterator &rhsObj) const;
bool operator!=(const LL_iterator &rhsObj) const;
}; //END class LL_iterator我只是不知道如何为这样的函数(返回模板类型的函数)编写函数声明。
提前感谢!
发布于 2015-03-29 23:25:49
您至少需要在定义LLIterator类之前声明LL模板,以便能够在成员函数的声明中使用它。在第一次引用LLIterator<T>时,编译器不知道LLIterator的名称。
发布于 2015-03-29 23:26:48
您可以在不定义类模板的情况下声明类模板,就像对类、函数和变量一样:
template <typename> class Bar;
template <typename T> class Foo
{
inline Bar<T> DoSomething(); // "inline" now has to be explicit
};
template <typename T> class Bar
{
Foo<T> DoAnotherThing() // implicitly "inline"
{
// implementation, may use Foo<T> as complete
}
};
template <typename T>
Bar<T> Foo<T>::DoSomething()
{
// implementation, now Bar<T> is complete, too
}这样,您就可以完成相互依赖的类模板的定义(因为成员函数返回和参数类型不需要在成员函数的声明点完成),以后还可以添加成员函数定义。
https://stackoverflow.com/questions/29336157
复制相似问题