"Undefined Reference to"

Oct 17, 2016 at 1:42pm
closed account (9GEqko23)
I know this seems like a repetitive question but I just started writing some code until I came across this error, any reason why?
Error:
 
main.cpp:(.text+0x38): undefined reference to `Championship<Player, 11>::Championship(int, char const*)' 


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
 #include <iostream>
#include "classes.h"
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
	Championship<Player, 11> ppChmp(atoi(argv[1]), "ping-pong");
	return 0;
}


classes.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef CLASSES_H
#define CLASSES_H

template<class T, int maxScore>
class Championship
{
	public:
		Championship(int, const char*); //Passes through game type and number of games
	private:
		char *compType;
		T *t; //Teams or Players
};
#endif 


Championship.cpp:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "classes.h"

using namespace std;

template<class T, int maxScore>
Championship<T, maxScore>::Championship(int s, const char* name)
{
	//Code Here
}
Oct 17, 2016 at 2:18pm
A template class is ignored until it is used with its template parameter (line 9 in main.cpp). At that point the compiler sees only the declarations within classes.h and not the definitions in Championship.cpp. Therefore the templated defintions won't be include in the compiler process.
Oct 17, 2016 at 2:29pm
linker is expecting
1
2
3
4
Championship<Player, 11>::Championship(int s, const char* name)
{
	//Code Here
}


(assuming you have defined Player someplace and linking Championship.cpp with main.cpp)

else
1
2
3
4
5
6
7
8
9
10
template<class T, class U>
class Championship
{
public:

	Championship(int, const char*); //Passes through game type and number of games
private:
	char *compType;
	T *t; //Teams or Players
};


and

1
2
3
4
Championship<Player, int>::Championship(int s, const char* name)
{
	//Code Here
}


and
 
Championship<Player, int> ppChmp(atoi(argv[1]), "ping-pong");
Topic archived. No new replies allowed.