strange template error

R

reinisr

Hi!

This program:

template<class T_index, class T_data>
class AAA
{
public:
AAA(int ii = 0,int jj = 0) {}
};

AAA<int, int> a1(10, 20); // this works
AAA<int, int> a3(10, 20); // this works

class BBB
{
AAA<int, int> a4; // this works
AAA<int, int> a5(10, 20); // this doesn't work
};

int main()
{
return 0;
}

fails to compile, I tried
g++296 and g++32 give errors:
test.cpp:17: invalid data member initiailization
test.cpp:17: use `=' to initialize static data members

g++34 g++4 give errors:
test.cpp:17: error: expected identifier before numeric constant
test.cpp:17: error: expected `,' or `...' before numeric constant
test.cpp:17: error: ISO C++ forbids declaration of `parameter' with no
type

I tried to play with typename keyword with no success.
I wonder what Bjarne Stroustrup is going to say about this mess.

Any ideas?

Reinis
 
D

dragoncoder

reinisr said:
Hi!

This program:

template<class T_index, class T_data>
class AAA
{
public:
AAA(int ii = 0,int jj = 0) {}
};

AAA<int, int> a1(10, 20); // this works
AAA<int, int> a3(10, 20); // this works

class BBB
{
AAA<int, int> a4; // this works

This is a declaration.
AAA<int, int> a5(10, 20); // this doesn't work

Here you are trying to define (initialize) a member object, you are not
supposed to do that inside the class body. You need to do this inside
BBB constructor.
 
R

red floyd

dragoncoder said:
This is a declaration.


Here you are trying to define (initialize) a member object, you are not
supposed to do that inside the class body. You need to do this inside
BBB constructor.

To be more precise, the compiler-generated default constructor for BBB
default constructs a4. a5 is a syntax error.

To do what you want:

class BBB
{
AAA<int, int> a4;
AAA<int, int> a5;
public:
BBB() : a4(), a5(10, 20) { }
};
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top