Templates and Inheritance

closed account (9GEqko23)
Hello, I was just wondering how I can get my classes to work using inheritance and templates, but I have this error:
1
2
classes.cpp: [Error] type/value mismatch at argument 1 in template parameter list for 'template<class T, int maxScore> class Championship'
classes.cpp: [Error] expected a type, got 'Player'

Here is my code, any help would be appreciated, thanks.

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

using namespace std;

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


classes.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "classes.h"

using namespace std;

template<class T, int maxScore>
Championship<T, maxScore>::Championship(int s, const char* name)
{
	//Code Here
}

template<class T, int maxScore>
Player<T, maxScore>::Player()
{
	//Code Here
}

template class Championship<Player, 11 >;


classes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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
};

template<class T, int maxScore>
class Player: public Championship<T, maxScore>
{
	public:
		Player();
	private:
		int title;
		int price;
};

#endif 


I get the error on this line:
 
template class Championship<Player, 11 >;
Player requires template paramter as well, since it inherits from Championship (which is very likely not right).
closed account (9GEqko23)
So how would I type up that? What do I need to change on the line?
template class Championship<Player<Player, 11>, 11>
Or something similar - I don't know what you're thinking - to me, this is broken.

I doubt that the strong relationship "A player is a championship" implied by the inheritance of Championship from Player is correct. Why is Player a template?

Also -- template definitions must be visible from the point of instantiation. So you need to move the implementation of your templates out of the source file and into the header file -- your code won't link, otherwise.
Last edited on
TheAussieGermanBoy wrote:
So how would I type up that? What do I need to change on the line?
Do not inherit Player from Championship and it does not need to be be templated class either.

Actually template makes only sense when you're doing something with the template parameter.
Topic archived. No new replies allowed.