'之前的不合格ID错误代币

时间:2016-04-16 19:13:57

标签: 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>::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;

}

可能出现什么问题?我知道它可能与模板有关的简单。

由于

1 个答案:

答案 0 :(得分:0)

在graph.hpp片段中,将代码更改为

#include "graph.h"

template <typename T>
graph<T>::graph(){    //Constructor has no return type.
   numVertices = 0;
   graphWeight = 0;
}

不幸的是,您无法真正拥有模板的单独标头和实现文件,因此请将实现放在头文件中。有关更多信息和解决方法,请参阅Why can templates only be implemented in the header file?