VS 为这段代码抛出奇怪的编译器错误,在第 9 行给我 3 个错误。 我以前在其他项目中使用过类似的代码,并且运行良好。 Node 类包含在 header 中,两个指针在构造函数中都设置为 nullptr。
template<class T>
class Edge
{
public:
Edge<T> *next;
Node<T> *destination;
Edge<T>();
~Edge();
};
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 ';'
我假设最后 2 个错误与第一个错误有某种关联,所以我猜测有一个小的语法问题导致了所有 3 个问题。 就像我说的,我以前做过类似的事情,没有任何问题,所以这让我很困惑。
最佳答案
您是否声明了 Node?编译器需要在 Edge 之前了解节点。
这compiles :
#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;
}
Success time: 0 memory: 15240 signal:0
0x7ffd75720b70
关于c++ - 模板类的 VS 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43660830/