C++ 模板类的VS编译器错误
VS为这段代码抛出了奇怪的编译器错误,在第9行给了我3个错误。 我以前在其他项目中使用过类似的代码,效果很好。 节点类包含在头中,两个指针在构造函数中都设置为nullptrC++ 模板类的VS编译器错误,c++,templates,visual-c++,C++,Templates,Visual C++,VS为这段代码抛出了奇怪的编译器错误,在第9行给了我3个错误。 我以前在其他项目中使用过类似的代码,效果很好。 节点类包含在头中,两个指针在构造函数中都设置为nullptr template<class T> class Edge { public: Edge<T> *next; Node<T> *destination; Edge<T>(); ~Edge(); }; 模板 阶级边缘 { 公众: 边*下一步; 节点
template<class T>
class Edge
{
public:
Edge<T> *next;
Node<T> *destination;
Edge<T>();
~Edge();
};
模板
阶级边缘
{
公众:
边*下一步;
节点*目的地;
边();
~Edge();
};
错误C2143:语法错误:缺少“;”在“之前,您是否声明了节点?编译器需要在边缘之前了解节点
这:
#包括
使用名称空间std;
//使用节点的正向声明,以便编译器知道此类型存在。
模板类节点;
模板
阶级边缘
{
公众:
边*下一步;
节点*目的地;
边(){};
~Edge(){};
};
int main(){
边缘测试;
std::cout是否声明了节点?编译器需要在Edge之前了解节点
这:
#包括
使用名称空间std;
//使用节点的正向声明,以便编译器知道此类型存在。
模板类节点;
模板
阶级边缘
{
公众:
边*下一步;
节点*目的地;
边(){};
~Edge(){};
};
int main(){
边缘测试;
标准::cout
error C2143: syntax error: missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'
#include <iostream>
using namespace std;
// Use a forward-declaration of Node, so that the compiler knows this type exists.
template<class T> class Node;
template<class T>
class Edge
{
public:
Edge<T> *next;
Node<T> *destination;
Edge<T>(){};
~Edge(){};
};
int main() {
Edge<int> test;
std::cout<<&test<<std::endl;
return 0;
}