templates

Hey I've just started learning about templates. I neede to create a template for an array.
this is the code I wrote:
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
52
53
54
55
#include <iostream>

using namespace std;

template <class T> class Array
{
private:
	T* data;
	unsigned int size;

public:
	Array<T> (unsigned int x)
	{
		size = x;
	}
	
	const T& operator[] (unsigned int index) const
	{
		if (index < 0 || index >= size () )
			throw domain_error("Array index out of bounds");
		return data[index];
	}

	unsigned int getSize ()  { return size; }

	void add (const T&, unsigned int index)
	{
		if (index < 0 || index >= size () )
			throw domain_error("Array index out of bounds");
		else
			data[index] = T;
	}

	
	void display ()
	{
		cout << "[" << data[0];
		for (int i = 1; i < size < ++i)
			cout << " , " <<data[i] 
		cout << "]" << endl;
	}
	
};

int main ()
{
	Array<int> arr(10);

	for (int i = 0; i <arr.getSize(); ++i)
		arr.add(i,i);

	arr.display();

	return 0;
}


and these are the errors I got:
1>testarray.cpp(50): warning C4018: '<' : signed/unsigned mismatch
1>testarray.cpp(29): error C2064: term does not evaluate to a function taking 0 arguments
1>testarray.cpp(28) : while compiling class template member function 'void Array<T>::add(const T &,unsigned int)'
1> with
1> [
1> T=int
1> ]
1>testarray.cpp(48) : see reference to class template instantiation 'Array<T>' being compiled
1> with
1> [
1> T=int
1> ]
1>testarray.cpp(32): error C2275: 'T' : illegal use of this type as an expression
1>testarray.cpp(48) : see declaration of 'T'

Firstly, I don't really understand the errors and I'll be glad if someone could explain the, to me. Also I'm not really sure why I'm getting there errors (I'm working with a book, and my code is pretty close to the one in the book)
thanks!
line 19
There is no way index can be less than 0 because it's unsigned. size is not a function so use size without parentheses or use getSize().

line 31
You are trying to assign a type which doesn't make sense. I guess you intended to do data[index] = T();.

line 38
The second less than sign should be a semicolon.

line 39
Missing semicolon at the end of the line.
Last edited on
Oh silly mistakes.. I fixed them.. still won't work though.
so the code now is:
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
52
53
54
55
56
57
#include <iostream>


using namespace std;

template <class T> class Array
{
private:
	T* data;
	unsigned int size;

public:
	Array<T> (unsigned int x)
	{
		size = x;
	}
	
	const T& operator[] (int index) const
	{
		if (index < 0||index >= size () )
			throw domain_error("Array index out of bounds");
		return data[index];
	}

	unsigned int getSize ()  { return size; }

	void add (const T&, int index)
	{
		if (index < 0 || index >= size () )
			throw domain_error("Array index out of bounds");
		else
			data[index] = T();
	}

	
	void display ()
	{
		cout << "[" << data[0];
		for (int i = 1; i < size ; ++i)
			cout << " , " <<data[i];
		cout << "]" << endl;
	}
	
};

int main ()
{
	Array<int> arr(10);

	for (int i = 0; i <(int)arr.getSize(); ++i)
		arr.add(i,i);

	arr.display();


	return 0;
}


testarray.cpp(29): error C2064: term does not evaluate to a function taking 0 arguments
testarray.cpp(28) : while compiling class template member function 'void Array<T>::add(const T &,int)'
1> with
1> [
1> T=int
1> ]
testarray.cpp(48) : see reference to class template instantiation 'Array<T>' being compiled
1> with
1> [
1> T=int
1> ]

Also what does T() do?
#include <stdexcept>

Also what does T() do?

It creates a value-initialized temporary object of type T.
still getting the same errors :(

 
if (index < 0 || index >= size() )


Where is the function size() defined?
okay so now it's working.
But if I add to the main class the following: cout << arr[0] << endl; I get building mistakes
Here is something you need to know; if you tell us what the error is, we can use that information to help you. If you do not tell us the error messages and just say "it doesn't work" or "there are mistakes" it is much harder for us to help you.

This builds fine:
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
52
53
54
55
56
57
#include <iostream>
#include <stdexcept>

using namespace std;

template <class T> class Array
{
private:
	T* data;
	unsigned int size;

public:
	Array<T> (unsigned int x)
	{
		size = x;
	}
	
	const T& operator[] (int index) const
	{
		if (index < 0||index >= size )
			throw domain_error("Array index out of bounds");
		return data[index];
	}

	unsigned int getSize ()  { return size; }

	void add (const T&, int index)
	{
		if (index < 0 || index >= size)
			throw domain_error("Array index out of bounds");
		else
			data[index] = T();
	}

	
	void display ()
	{
		cout << "[" << data[0];
		for (int i = 1; i < size ; ++i)
			cout << " , " <<data[i];
		cout << "]" << endl;
	}
	
};

int main ()
{
	Array<int> arr(10);

	for (int i = 0; i <(int)arr.getSize(); ++i)
		arr.add(i,i);

	arr.display();
        cout << arr[0] << endl;

	return 0;
}



Note that the pointer data is never made to point at anything, so there's a good chance that you'll segFault when you try to use it.
Last edited on
testarray.cpp(22): error C2064: term does not evaluate to a function taking 0 arguments
testarray.cpp(21) : while compiling class template member function 'const int &Array<T>::operator [](int) const'
1> with
1> [
1> T=int
1> ]
testarray.cpp(50) : see reference to class template instantiation 'Array<T>' being compiled
1> with
1> [
1> T=int
1> ]
What are you using to compile your code?
visual c++ express 2010
gcc compiles it without warning.

http://ideone.com/6wqXpg
Topic archived. No new replies allowed.