Something wrong with templates

I'm trying to program a simple add/remove program using templates as well.
I'm very new to templates, so please help me out here.

ERROR: unable to match function definition to an existing declaration
(_says error is on line 54_)

CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;

struct thing
{
};

template <class T>
class list
{
 private:
  #define MAXSIZE 50
	thing ar[MAXSIZE];
	int number;
 public:
	list();
	bool isFull();
	bool isEmpty();
	void add(thing x);
	thing remove();
	/* 
	  purpose:      remove one thing from the list
	  precondition: list not empty;
	  postcondition: list contains one less thing and it is returned back
	*/
};

template <class T>
list<T>::list()
{
	number = 0;
}

template <class T>
bool list<T>::isEmpty()
{
	return (number == 0);
}

template <class T>
bool list<T>::isFull()
{
	return (number == MAXSIZE);
}

template <class T>
void list<T>::add(T x)
{
	ar[number] = x;
	number++;
}
Hello,

I think, the line 19, should be : "void add(T x);" instead of "void add(thing x);"
Last edited on
Same on line 13 I believe.
Eh,

now I am getting these errors:

Error 2 error LNK1120: 1 unresolved externals 1

Error 1 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
I did not seen the line 13 ;).


But, is there some code missing ?

You did not defined the remove() function ?

I see it is declared as "thing remove();" but as there is no template later in the code this should be "thing remove(){};" or "T remove(){};" no ?
^Yeah.

As for your error. You have no main()...add one.
No, I did not finish the remove function yet.

In the class,
Does thing remove(); have to be T remove(); as well?
Not necessarily T as return type, but (stop me if i tell something wrong) I think you need to define the content of the function either by adding {} in the class definition or by writing the function later in the code.
Yeah, that's where I'm getting confused.
I just don't know what I need to put when I make the function.

thing list::remove()?

Basically, my professor wants me to use this class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Class list
{
 private:
  #define MAXSIZE 50
	thing ar[MAXSIZE];
	int number; // number of things in a.

 public:
	list();
	bool isFull();
	// returns true if the list is full, false otherwise

	bool isEmpty();
	// returns true if the list is empty, false otherwise

	void add(thing x);
	/*
	  purpose:      add x to the list
	  precondition: list not full;
	  postcondition: list contains one more thing, i.e. x
	*/

	thing remove();
	/* 
	  purpose:      remove one thing from the list
	  precondition: list not empty;
	  postcondition: list contains one less thing and it is returned back
	*/
};



And finish the rest of the code and then change it to templates.
Last edited on
Well try to compile it with T remove() {}; instead of thing remove(); line 20.

OR change line 20 to T remove(); and add later in your code the following function definition :

1
2
3
4
5
template <class T>
T list<T>::remove()
{
	// your stuff... return the T type object removed from the list.
}
Last edited on
Ahh okay, yeah it compiles fine now!
Thanks everyone!

Nice ;)

Mark it as solved if you think your problem is solved.
Topic archived. No new replies allowed.