-5

I am new to stack overflow and I would appreciate feedback. I am creating a template graph class and have split it into two files graph.h and graph.hpp, but everytime I compile I get this error:

    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

Here is my graph.hpp file so far:

#include "graph.h"

//template <typename T>
typename graph<T>::graph(){
   numVertices = 0;
   graphWeight = 0;
}

And my graph.h file looks something like:

template <typename T>
class graph{

   public:
      graph();
.
.
.
   private:



};

Also, my main.cpp is just simply:

#include "graph.h"

int main(){
   graph<int> g;

}

What could possibly be wrong? I know it's probably something simple that has to do with the template.

Thanks

J. Doe
  • 11
  • 3

1 Answers1

0

In your graph.hpp snippet, change your code to this

#include "graph.h"

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

Unfortunately, you have cannot really have separate header and implementation file for templates, so put the implementation in your header file. For more info and workarounds, see Why can templates only be implemented in the header file?

Community
  • 1
  • 1
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68