Templates and g++

Discussion in 'C++' started by ArbolOne, Sep 9, 2012.

  1. ArbolOne

    ArbolOne Guest

    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?
     
    ArbolOne, Sep 9, 2012
    #1
    1. Advertising

  2. ArbolOne

    Dombo Guest

    Op 09-Sep-12 23:30, ArbolOne schreef:
    > MinGW - GNU c++
    > main.cpp
    > ~~~~~~~~
    > template <typedef T>

    ^^^^^^^

    Use typename or class here.
     
    Dombo, Sep 9, 2012
    #2
    1. Advertising

  3. ArbolOne

    Marc Guest

    ArbolOne wrote:

    > 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.

    > ...\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
     
    Marc, Sep 9, 2012
    #3
  4. ArbolOne

    Luca Risolia Guest

    On 09/09/2012 23:35, Marc wrote:
    > ArbolOne wrote:
    >> template <typedef T>

    >
    > What is "typedef" doing there? Did you mean "class" or "typename"?
    >
    >> class List{
    >> T& getData(){ return T;}


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

    >> }

    >
    > Missing ';'.
     
    Luca Risolia, Sep 9, 2012
    #4
  5. ArbolOne

    ArbolOne Guest

    Thanks folks, I got it now.


    On Sunday, September 9, 2012 5:30:01 PM UTC-4, ArbolOne wrote:
    > 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?
     
    ArbolOne, Sep 9, 2012
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred
    Replies:
    1
    Views:
    611
    Neredbojias
    Sep 26, 2005
  2. JKop
    Replies:
    3
    Views:
    484
  3. Generic Usenet Account
    Replies:
    3
    Views:
    832
    Generic Usenet Account
    Jul 14, 2005
  4. BigMan
    Replies:
    1
    Views:
    440
  5. recover
    Replies:
    2
    Views:
    822
    recover
    Jul 25, 2006
Loading...

Share This Page