By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

Member function redeclaration not allowed when constructing temporary object

P: n/a
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

Apr 20 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Marco Wedekind wrote:
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

Apr 20 '06 #2

P: n/a
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

Apr 20 '06 #3

P: n/a
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

Apr 20 '06 #4

P: n/a
Marco Wedekind wrote:
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
Apr 20 '06 #5

P: n/a
Hello Tom,

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

Best regards

Marco

Apr 20 '06 #6

P: n/a
Marco Wedekind wrote:
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

Apr 20 '06 #7

P: n/a
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

Apr 21 '06 #8

P: n/a
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...

Apr 21 '06 #9

P: n/a

Marco Wedekind wrote:
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
}

Apr 21 '06 #10

P: n/a

Tom Widmer wrote:
Marco Wedekind wrote:
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


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

Apr 21 '06 #11

P: n/a
> 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

Apr 26 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.