Member function redeclaration not allowed when constructing temporary object

M

Marco Wedekind

Hello all,

I have a strange compiler behaviour with this code:

---- Begin of code snippet ----

class Base
{
public:
static unsigned int ClassId();

};

class Derived
{
public:
static unsigned int ClassId() { return 2; };

};

class Test
{
public:
Test(unsigned int p1) {};

};

int main(int argc, char * argv[])
{
// This works
Test a(Derived::ClassId());

// This crashes
Test(Derived::ClassId());

return 0;

}

---- End of code snippet ----

The line

Test(Derived::ClassId());

failes to compile with gcc 3.3.5 and Visual Studio 8. With gcc I get
the following error message:

test.cpp: In function `int main(int, char**)':
test.cpp:24: error: prototype for `Test Derived::ClassId()' does not
match any
in class `Derived'
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassId()
test.cpp:24: error: `Test Derived::ClassId()' and `static unsigned int
Derived::ClassId()' cannot be overloaded
test.cpp:24: error: prototype for `Test Derived::ClassId()' does not
match any
in class `Derived'
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassId()
test.cpp:24: error: `Test Derived::ClassId()' and `static unsigned int
Derived::ClassId()' cannot be overloaded
test.cpp:24: error: declaration of `Test Derived::ClassId()' outside of
class
is not definition

So why does it fail with a temporary object in such a way? Should the
compiler not handle this case equivalent to the "non-temporary case"?
Do you have any hints on this behaviour?

Thanks and best regards

Marco
 
M

mlimber

Marco said:
Hello all,

I have a strange compiler behaviour with this code:

---- Begin of code snippet ----

class Base
{
public:
static unsigned int ClassId();

};

class Derived
{
public:
static unsigned int ClassId() { return 2; };

};

class Test
{
public:
Test(unsigned int p1) {};

};

int main(int argc, char * argv[])
{
// This works
Test a(Derived::ClassId());

// This crashes
Test(Derived::ClassId());

return 0;

}

---- End of code snippet ----

The line

Test(Derived::ClassId());

failes to compile with gcc 3.3.5 and Visual Studio 8. With gcc I get
the following error message:

test.cpp: In function `int main(int, char**)':
test.cpp:24: error: prototype for `Test Derived::ClassId()' does not
match any
in class `Derived'
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassId()
test.cpp:24: error: `Test Derived::ClassId()' and `static unsigned int
Derived::ClassId()' cannot be overloaded
test.cpp:24: error: prototype for `Test Derived::ClassId()' does not
match any
in class `Derived'
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassId()
test.cpp:24: error: `Test Derived::ClassId()' and `static unsigned int
Derived::ClassId()' cannot be overloaded
test.cpp:24: error: declaration of `Test Derived::ClassId()' outside of
class
is not definition

So why does it fail with a temporary object in such a way? Should the
compiler not handle this case equivalent to the "non-temporary case"?
Do you have any hints on this behaviour?

Thanks and best regards

Marco

The compiler is interpreting the second line as a function prototype
rather than a temporary object instantiation (note that it dropped the
parentheses in the error message). The rule is that if a statement can
be interpreted as a prototype, it will be.

Cheers! --M
 
M

Marco Wedekind

Hello,

thanks for your reply!

Do you have any suggestion on how to circumvent this problem
syntactically? What would be the most elegant solution?

Best regards

Marco
 
M

Marco Wedekind

One thing I still do not understand is: Why does it drop the
parentheses? If the compiler would keep them, I do not see how the
line:

Test(Derived::ClassId());

could be interpreted as a function prototype...

Best regards

Marco
 
T

Tom Widmer

Marco said:
One thing I still do not understand is: Why does it drop the
parentheses?

C declaration syntax compatibility. C allowed extraneous parentheses,
hence C++ does too. e.g.
int (i);
is legal in C and C++ as a declaration of an integer, i.

Tom
 
M

Marco Wedekind

Hello Tom,

thanks for this insight :) This is something I have not been aware of
in C++ development.

Best regards

Marco
 
M

mlimber

Marco said:
Hello,

thanks for your reply!

Do you have any suggestion on how to circumvent this problem
syntactically? What would be the most elegant solution?

Best regards

Marco

First, please quote sufficient context of the message you are replying
to. It makes it easier for others to follow the conversation.

To answer your question: It depends on what you're really trying to do.
You could just use a named object like your working example above, and
if you want it to go out of scope immediately, you could put it in
braces. If you are passing the temporary object to a function or
something similar, it shouldn't be interpreted as a prototype in the
first place.

If you show us what you are really trying to do, we might be able to
help more.

Cheers! --M
 
M

Marco Wedekind

Hello,
First, please quote sufficient context of the message you are replying
to. It makes it easier for others to follow the conversation.
...
You could just use a named object like your working example above, and
if you want it to go out of scope immediately, you could put it in
braces. If you are passing the temporary object to a function or
something similar, it shouldn't be interpreted as a prototype in the
first place.

You are right, I am going to quote better now ...

Your first solution sounds good to me. The second one (passing
temporary to function) points out some new aspect of my problem to me.
In my current project I have a problem with calling a function on a
temporary object like this:

Test(Derived::ClassId()).someFunction();

It compiles well with Visual Studio 8, but fails to compile with gcc
3.3.5. This is the actual problem which made me write to this group.
The modified example below reproduces this behaviour:

class use
{
void someOtherFunction()
{
Test(Derived::ClassId()).someFunction();
}
};

The error message I am getting with gcc now is:

error: cannot declare member function 'Test Derived::ClassId()' within
'use'

So I am trying to call someFunction() on the temporary object. The
compiler however seems to try to declare a member function at this
point, like you have described earlier in this thread.

I wonder why gcc tries to declare a function there, because I cannot
"call" someFunction() on a function declaration...

One work-around for me was to static_cast<> the return value of
Test::ClassId() to its type.

Best regards

Marco
 
M

Marco Wedekind

There was a slight mistake on my side: The error message of gcc is:

error: cannot declare member function `Derived::ClassId' within `use'

without Test as a return type...
 
F

Fei Liu

Marco said:
Hello all,

I have a strange compiler behaviour with this code:

---- Begin of code snippet ----

class Base
{
public:
static unsigned int ClassId();

};

class Derived
{
public:
static unsigned int ClassId() { return 2; };

};

class Test
{
public:
Test(unsigned int p1) {};

};

int main(int argc, char * argv[])
{
// This works
Test a(Derived::ClassId());

// This crashes
Test(Derived::ClassId());

return 0;

}

---- End of code snippet ----

The line

Test(Derived::ClassId());

failes to compile with gcc 3.3.5 and Visual Studio 8. With gcc I get
the following error message:

test.cpp: In function `int main(int, char**)':
test.cpp:24: error: prototype for `Test Derived::ClassId()' does not
match any
in class `Derived'
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassId()
test.cpp:24: error: `Test Derived::ClassId()' and `static unsigned int
Derived::ClassId()' cannot be overloaded
test.cpp:24: error: prototype for `Test Derived::ClassId()' does not
match any
in class `Derived'
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassId()
test.cpp:24: error: `Test Derived::ClassId()' and `static unsigned int
Derived::ClassId()' cannot be overloaded
test.cpp:24: error: declaration of `Test Derived::ClassId()' outside of
class
is not definition

So why does it fail with a temporary object in such a way? Should the
compiler not handle this case equivalent to the "non-temporary case"?
Do you have any hints on this behaviour?

Thanks and best regards

Marco

The following code only produces compile err on line 10. Line 5 is
clear, it declares a class C object c; Line 8 declares a class C object
C?! definitely a bad idea to write code like that. line 6,7 both are
acceptable to declare a pointer to class C object, but what's the
difference beteween 'new C' and 'new C()'? Line 9 uses C declared on
line 8.. Line 10 is probably the same deal, the compiler considers C()
as a prototype but couldn't find a return type for the function
declaration: (error: call of an object of a class type without
appropriate operator() or conversion functions to
pointer-to-function type)

class C{
};

void bar(){
C c;
C * d = new C;
C * e = new C(); // what's different between new C and new C()?
C C;
C;
C(); // error
}
 
M

Michiel.Salters

Tom said:
C declaration syntax compatibility. C allowed extraneous parentheses,
hence C++ does too. e.g.
int (i);
is legal in C and C++ as a declaration of an integer, i.

Tom

Please keep the original text - I'd have to lookit up myself.

Anyway, what you had is Test(Derived::ClassId()); in which the
outer parantheses can be dropped. However, the grammar for declarations
allows just a single pair. Expressions allow any number, so the
expression
Test((Derived::ClassId())); can be interpreted in only one way - as the
creation of a temporary Test from the expression (Derived::ClassId()).

HTH,
Michiel Salters
 
M

Marco Wedekind

Anyway, what you had is Test(Derived::ClassId()); in which the
outer parantheses can be dropped. However, the grammar for declarations
allows just a single pair. Expressions allow any number, so the
expression
Test((Derived::ClassId())); can be interpreted in only one way - as the
creation of a temporary Test from the expression (Derived::ClassId()).

Hello Michiel,

thanks for your reply. It works, although it just looks a little odd to
me. I will have to remember this...

Thanks for all your replies

Marco
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top