C++ “')”标记之前的非限定id错误
我是新的堆栈溢出,我将感谢反馈。我正在创建一个模板图类,并将其拆分为两个文件graph.h和graph.hpp,但每次编译时都会出现以下错误:C++ “')”标记之前的非限定id错误,c++,class,graph,C++,Class,Graph,我是新的堆栈溢出,我将感谢反馈。我正在创建一个模板图类,并将其拆分为两个文件graph.h和graph.hpp,但每次编译时都会出现以下错误: In file included from graph.h:97:0, from main.cpp:2: graph.hpp:4:26: error: expected unqualified-id before ‘)’ token typename graph<T>::gr
In file included from graph.h:97:0,
from main.cpp:2:
graph.hpp:4:26: error: expected unqualified-id before ‘)’ token
typename graph<T>::graph(){
^
makefile:2: recipe for target 'executable.x' failed
以下是我目前的graph.hpp文件:
#include "graph.h"
//template <typename T>
typename graph<T>::graph(){
numVertices = 0;
graphWeight = 0;
}
我的graph.h文件看起来像:
template <typename T>
class graph{
public:
graph();
.
.
.
private:
};
另外,我的main.cpp只是简单地:
#include "graph.h"
int main(){
graph<int> g;
}
可能出了什么问题?我知道这可能与模板有关
谢谢在graph.hpp代码段中,将代码更改为
#include "graph.h"
template <typename T>
graph<T>::graph(){ //Constructor has no return type.
numVertices = 0;
graphWeight = 0;
}
不幸的是,您不能真正为模板提供单独的头文件和实现文件,所以请将实现放在头文件中。有关更多信息和解决方法,请参见取消注释模板,删除下面的typename。谢谢!我会调查的!