Templates and typedef

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bolin

    Templates and typedef

    I am trying to compile the following dummy program using VC++6.0:



    template < typename T >
    class A
    {
    public:
    A(T value) { m_value = value; }
    T getValue() { return m_value; }

    public:
    typedef T Type;

    private:
    T m_value;
    };


    int main(int argc, char * argv[])
    {
    {
    A<double> a(9)
    A<double>::Typ e val = a.getValue();
    }

    return 0;
    }

    but when I do, I get the following:


    C:\Program Files\Microsoft Visual
    Studio\MyProjec ts\testInClassT ypedef\main.cpp (43) : error C2143:
    syntax error : missing ';' before 'tag::id'
    C:\Program Files\Microsoft Visual
    Studio\MyProjec ts\testInClassT ypedef\main.cpp (43) : error C2146:
    syntax error : missing ';' before identifier 'val'
    C:\Program Files\Microsoft Visual
    Studio\MyProjec ts\testInClassT ypedef\main.cpp (43) : error C2275:
    'A<double>::Typ e' : illegal use of this type as an expression
    C:\Program Files\Microsoft Visual
    Studio\MyProjec ts\testInClassT ypedef\main.cpp (43) : error C2065: 'val'
    : undeclared identifier


    I was wondering if I was doing something wrong according to C++, or
    according to VC++6.0.

    Thanks

    G.
  • Jonathan Turkanis

    #2
    Re: Templates and typedef


    "Bolin" <gao_bolin@voil a.fr> wrote in message
    news:93c5215b.0 402091551.46461 ffc@posting.goo gle.com...[color=blue]
    > I am trying to compile the following dummy program using VC++6.0:
    >
    >
    >
    > template < typename T >
    > class A
    > {
    > public:
    > A(T value) { m_value = value; }
    > T getValue() { return m_value; }
    >
    > public:
    > typedef T Type;
    >
    > private:
    > T m_value;
    > };
    >
    >
    > int main(int argc, char * argv[])
    > {
    > {
    > A<double> a(9)
    > A<double>::Typ e val = a.getValue();
    > }
    >
    > return 0;
    > }[/color]

    The main problem is that you have omitted the semicolon after 'a(9)'.
    When you post error messages, its good to point out which lines the
    line numbers refer to.

    Your code it unidiomaic in several respects. In the constuctor, you
    probably should initialize m_value in an initializer list. Also, you
    probably should write a const accessor method in addition to the
    non-const one. Depending on what types you plan to use as template
    arguments, you might think about passing by value instead of
    reference. You might also consifer whether a default constructor is
    appropriate.

    Jonathan


    Comment

    Working...