c++ - 模板类的 VS 编译器错误

标签 c++ templates visual-c++

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/

相关文章:

c++ - 如何使用具有递归模板函数的线程

c++ - Release模式错误但构建模式不错误

c++ - Visual Studio项目中的 "Platform Toolset"设置是什么

c++ - 如何设置专用 GPU 以对 CUDA 内核进行基准测试?

C++ GLSL 着色器 : "error: GLSL 3.30 is not supported. Supported versions are: 1.10, 1.20, 1.30, 1.00 ES, and 3.00 ES" or black window

c++ - 从 C++ 登录并使用 Rails

c++ - 重载 += 或 -= 运算符的好处

c++ - 有选择地覆盖模板类的功能

c++ - 使用 C++/Visual C++ 控制光标和键盘

C++ 结构 : reference to overloaded function could not be resolved