Hi I got a problem with including header files with Visual Studio. The code is here:
Graph.h
#pragma once
#include <iostream>
template<typename T>
class Graph
{
public:
Graph(int s);
void print_graph() const;
protected:
T** graph;
int size;
};
Graph.cpp
#include "Graph.h"
template<typename T>
Graph<T>::Graph(int s)
{
size = s;
// create 2D array
graph = new T * [size];
for (int i = 0; i < size; i++)
{
graph[i] = new T[size];
}
}
template<typename T>
void Graph<T>::print_graph() const
{
// print the bool matrix
std::cout << "Graph: \n";
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
std::cout << graph[i][j] << " ";
std::cout << std::endl;
}
}
main cpp
#include <iostream>
#include "Graph.h"
int main()
{
Graph<int> g(10);
g.print_graph();
return 0;
}
- I always get error messages like, Error LNK2019 unresolved external symbol "public: __cdecl Graph::Graph(int)" (??0?$Graph@H@@QEAA@H@Z) referenced in function main.
- If I put everything in only one single main file (no need to include at all), everything works.
- But surprisingly, if I change #include "Graph.h" to #include "Graph.cpp", the program compiles. Thanks very much for your help!