Templates and g++

A

ArbolOne

MinGW - GNU c++
main.cpp
~~~~~~~~
template <typedef T>
class List{
private:
size_t id;
T data;

public:
T& getData(){ return T;}
int getId(){return id;}
void setData(T const & d){data = d;}
void setId(const int i){ id = i;}
}
int main(){
return 0;
}
I am learing how to use c++ templates, but this simple example gives me the following error:
....\Templates\main.cpp:1:19: error: 'T' does not name a type
....\Templates\main.cpp:12:1: error: ISO C++ forbids declaration of 'parameter' with no type [-fpermissive]
....\Templates\main.cpp:12:1: error: typedef declaration invalid in parameter declaration
....\Templates\main.cpp:13:1: error: expected '>' before 'int'
....\Templates\main.cpp:13:11: error: expected unqualified-id before '{' token
Process terminated with status 1 (0 minutes, 0 seconds)
5 errors, 0 warnings

AFAIK g++ supports templates, so there must be something wrong with the code, any body?
 
M

Marc

ArbolOne said:
MinGW - GNU c++
main.cpp
~~~~~~~~
template <typedef T>

What is "typedef" doing there? Did you mean "class" or "typename"?
class List{
private:
size_t id;
T data;

public:
T& getData(){ return T;}
int getId(){return id;}
void setData(T const & d){data = d;}
void setId(const int i){ id = i;}
}

Missing ';'.
int main(){
return 0;
}
I am learing how to use c++ templates, but this simple example gives me the following error:
...\Templates\main.cpp:1:19: error: 'T' does not name a type
...\Templates\main.cpp:12:1: error: ISO C++ forbids declaration of 'parameter' with no type [-fpermissive]
...\Templates\main.cpp:12:1: error: typedef declaration invalid in parameter declaration

Yes, g++ agrees with me.
 
L

Luca Risolia

What is "typedef" doing there? Did you mean "class" or "typename"?

...and the above line is wrong as well: it's probably
T& getData(){ return data;}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

templates?? 2
Compiler error using g++ from MinGW 1
using my template class 11
Problem with codewars. 5
typedefs and templates 3
Half templates does not work. 6
Template Class 4
Error compiling templates 7

Members online

No members online now.

Forum statistics

Threads
473,818
Messages
2,569,727
Members
45,661
Latest member
NadineBour

Latest Threads

Top