| dorA26 (16) | |||
|
Hey I've just started learning about templates. I neede to create a template for an array. this is the code I wrote:
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! | |||
|
|
|||
| Peter87 (3687) | |
|
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
|
|
| dorA26 (16) | |||
|
Oh silly mistakes.. I fixed them.. still won't work though. so the code now is:
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? | |||
|
|
|||
| Peter87 (3687) | ||
#include <stdexcept>
It creates a value-initialized temporary object of type T. | ||
|
|
||
| dorA26 (16) | |
| still getting the same errors :( | |
|
|
|
| Moschops (5959) | |||
Where is the function size() defined? | |||
|
|
|||
| dorA26 (16) | |
|
okay so now it's working. But if I add to the main class the following: cout << arr[0] << endl; I get building mistakes | |
|
|
|
| Moschops (5959) | |||
|
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:
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
|
|||
| dorA26 (16) | |
|
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> ] | |
|
|
|
| Moschops (5959) | |
| What are you using to compile your code? | |
|
|
|
| dorA26 (16) | |
| visual c++ express 2010 | |
|
|
|
| Moschops (5959) | |
|
gcc compiles it without warning. http://ideone.com/6wqXpg | |
|
|
|