C++ 模板类返回类型?这是怎么写的?
我有两个模板类,第一个是:C++ 模板类返回类型?这是怎么写的?,c++,templates,C++,Templates,我有两个模板类,第一个是: template <typename T> class LL_iterator {...}; 模板 类LL_迭代器 {...}; 以及: template <class T> class LL {...} 模板 LL班 {...} 现在的问题是,我正在尝试在具有“LL_迭代器”返回类型的“LL”类中编写函数声明。“LL”类中的函数如下所示: LL_iterator<T> begin(); LL_迭代器begin();
template <typename T>
class LL_iterator
{...};
模板
类LL_迭代器
{...};
以及:
template <class T>
class LL
{...}
模板
LL班
{...}
现在的问题是,我正在尝试在具有“LL_迭代器”返回类型的“LL”类中编写函数声明。“LL”类中的函数如下所示:
LL_iterator<T> begin();
LL_迭代器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
错误1错误C2143:语法错误:缺少“;”在“之前,您至少需要声明LLIterator
模板,然后才能定义LL
类,以便在声明成员函数时使用它。当您第一次引用LLIterator
时,编译器不知道名称LLIterator
。您可以声明类模板而不定义它,就像您可以声明类、函数和变量一样:
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
}
模板类栏;
模板类Foo
{
inline Bar DoSomething();/“inline”现在必须是显式的
};
模板类栏
{
Foo DoAnotherThing()//隐式“内联”
{
//实现,可以使用Foo作为完成
}
};
模板
Bar Foo::DoSomething()
{
//实现,现在Bar也完成了
}
这样,您就可以完成相互依赖类模板的定义(因为成员函数返回和参数类型不需要在成员函数声明时完成),并且您可以稍后添加成员函数定义。请正确设置问题的格式。请设置代码的格式。不要忽略实时预览功能。将LL_iterator
模板定义移动到之前LL
LLiterator类是在LL类之后定义的,因此编译器无法识别数据类型。谢谢你的帮助!
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
}