Templates and typedef

B

Bolin

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>::Type val = a.getValue();
}

return 0;
}

but when I do, I get the following:


C:\Program Files\Microsoft Visual
Studio\MyProjects\testInClassTypedef\main.cpp(43) : error C2143:
syntax error : missing ';' before 'tag::id'
C:\Program Files\Microsoft Visual
Studio\MyProjects\testInClassTypedef\main.cpp(43) : error C2146:
syntax error : missing ';' before identifier 'val'
C:\Program Files\Microsoft Visual
Studio\MyProjects\testInClassTypedef\main.cpp(43) : error C2275:
'A<double>::Type' : illegal use of this type as an expression
C:\Program Files\Microsoft Visual
Studio\MyProjects\testInClassTypedef\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.
 
J

Jonathan Turkanis

Bolin said:
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>::Type val = a.getValue();
}

return 0;
}

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
 

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

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top