What's wrong with the following program?

P

PengYu.UT

Hi,

It seems that the line with "//no error" and "//error" are the same.
However, the second one give an error.

I'm using g++-3.3.
The error message was:

g++-3.3 -g -c -o main.o main.cc
main.cc: In function `int main(int, char**)':
main.cc:85: error: parse error before `,' token
make: *** [main.o] Error 1

Best wishes,
Peng


template <typename __Tp>
class A{
public:
A(){};
~A(){};
virtual void fun() = 0;
};

template <typename __Tp>
class A1 : public A<__Tp>{
public:
A1(){};
~A1(){};
virtual void fun() {};
};

template <typename __Tp>
class B{
public:
B(__Tp x, __Tp y, __Tp z){};
~B(){};
virtual void fun() = 0;
};

template <typename __Tp>
class B1 : public B<__Tp>{
public:
B1(__Tp x, __Tp y): B<__Tp>(x, x, y){};
~B1(){};
virtual void fun() {};
};


template <typename __Tp>
class D{
public:
D(const A<__Tp> &A_inst, const B<__Tp> &B_inst){};
~D(){};
};

int main(int argc, char *argv[])
{
const double x = 0;
A1<double> A1_inst;
B1<double> B1_inst(x, x);
//D<double> D_inst(A1_inst, B1_inst);//no error
D<double> D_inst(A1<double>, B1<double>(x,x));//error
}
 
E

E. Robert Tisdale

It seems that the line with "//no error" and "//error" are the same.
However, the second one give an error.

I'm using g++-3.3.
cat main.cpp
template <typename __Tp>
class A {
public:
A(void) { };
~A(void) { };
virtual void fun(void) = 0;
};

template <typename __Tp>
class A1: public A<__Tp> {
public:
A1(void) { };
~A1(void) { };
virtual void fun(void) { };
};

template <typename __Tp>
class B {
public:
B(__Tp x, __Tp y, __Tp z) { };
~B(void) { };
virtual void fun(void) = 0;
};

template <typename __Tp>
class B1: public B<__Tp> {
public:
B1(__Tp x, __Tp y): B<__Tp>(x, x, y) { };
~B1(void) { };
virtual void fun(void) {};
};


template <typename __Tp>
class D {
public:
D(const A<__Tp> &A_inst, const B<__Tp> &B_inst) { };
~D(void) { };
};

int main(int argc, char *argv[]) {
const double x = 0;
A1<double> A1_inst;
B1<double> B1_inst(x, x);
//D<double> D_inst(A1_inst, B1_inst);//no error
D<double> D_inst(A1<double>(), B1<double>(x, x));//error
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cpp
main.cpp: In instantiation of `A<double>':
main.cpp:10: instantiated from `A1<double>'
main.cpp:43: instantiated from here
main.cpp:2: warning: `class A<double>' \
has virtual functions but non-virtual destructor
main.cpp: In instantiation of `A1<double>':
main.cpp:43: instantiated from here
main.cpp:10: warning: `class A1<double>' \
has virtual functions but non-virtual destructor
main.cpp: In instantiation of `B<double>':
main.cpp:26: instantiated from `B1<double>'
main.cpp:44: instantiated from here
main.cpp:18: warning: `class B<double>' \
has virtual functions but non-virtual destructor
main.cpp: In instantiation of `B1<double>':
main.cpp:44: instantiated from here
g++ --version | head -1
g++ (GCC) 3.4.1
 
G

Gianni Mariani

Hi,

....

int main(int argc, char *argv[])
{
const double x = 0;
A1<double> A1_inst;
B1<double> B1_inst(x, x);
//D<double> D_inst(A1_inst, B1_inst);//no error
D<double> D_inst(A1<double>, B1<double>(x,x));//error
}

This is being interpreted as a declaration of a function named D_inst.

Try this:
D<double> D_inst(A1<double>(), B1<double>(x,x));

or this:
D<double> D_inst = D<double>(A1<double>(), B1<double>(x,x));
 
L

Larry I Smith

Gianni said:
Hi,

...

int main(int argc, char *argv[])
{
const double x = 0;
A1<double> A1_inst;
B1<double> B1_inst(x, x);
//D<double> D_inst(A1_inst, B1_inst);//no error
D<double> D_inst(A1<double>, B1<double>(x,x));//error
}

This is being interpreted as a declaration of a function named D_inst.

Try this:
D<double> D_inst(A1<double>(), B1<double>(x,x));

Using "g++ (GCC) 3.3.4 (pre 3.3.5 20040809)"
the above line produces this error:

"main.cpp:47: error: syntax error before `,' token"

Why?
or this:
D<double> D_inst = D<double>(A1<double>(), B1<double>(x,x));

This 2nd solution works. It's creating a temp D<double> then
copying it to D_inst. Why should that work, but your first
solution not work? You first solution calls the constructor
for A<double> directly, and looks perfectly legal.

Regards,
Larry
 
K

Kristo

Larry said:
Gianni said:
Hi,

...

int main(int argc, char *argv[])
{
const double x = 0;
A1<double> A1_inst;
B1<double> B1_inst(x, x);
//D<double> D_inst(A1_inst, B1_inst);//no error
D<double> D_inst(A1<double>, B1<double>(x,x));//error
}

This is being interpreted as a declaration of a function named
D_inst.

Try this:
D<double> D_inst(A1<double>(), B1<double>(x,x));

Using "g++ (GCC) 3.3.4 (pre 3.3.5 20040809)"
the above line produces this error:

"main.cpp:47: error: syntax error before `,' token"

Why?

Adding the empty parentheses hasn't changed anything; the compiler will
still parse it incorrectly. Try putting parentheses around the
temporary object:

D<double> D_inst((A1<double>()), B1<double>(x,x));

Incidentally, this is an instance of "C++'s Most Vexing Parse" from
Scott Meyers' book _Effective_STL_.

Kristo
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top