473,785 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Member function redeclaration not allowed when constructing temporary object

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::Clas sId());

// This crashes
Test(Derived::C lassId());

return 0;

}

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

The line

Test(Derived::C lassId());

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::ClassI d()' does not
match any
in class `Derived'
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassI d()
test.cpp:24: error: `Test Derived::ClassI d()' and `static unsigned int
Derived::ClassI d()' cannot be overloaded
test.cpp:24: error: prototype for `Test Derived::ClassI d()' does not
match any
in class `Derived'
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassI d()
test.cpp:24: error: `Test Derived::ClassI d()' and `static unsigned int
Derived::ClassI d()' cannot be overloaded
test.cpp:24: error: declaration of `Test Derived::ClassI d()' 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
11 11057
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::Clas sId());

// This crashes
Test(Derived::C lassId());

return 0;

}

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

The line

Test(Derived::C lassId());

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::ClassI d()' does not
match any
in class `Derived'
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassI d()
test.cpp:24: error: `Test Derived::ClassI d()' and `static unsigned int
Derived::ClassI d()' cannot be overloaded
test.cpp:24: error: prototype for `Test Derived::ClassI d()' does not
match any
in class `Derived'
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassI d()
test.cpp:24: error: `Test Derived::ClassI d()' and `static unsigned int
Derived::ClassI d()' cannot be overloaded
test.cpp:24: error: declaration of `Test Derived::ClassI d()' 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
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
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::C lassId());

could be interpreted as a function prototype...

Best regards

Marco

Apr 20 '06 #4
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
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
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
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::C lassId()).someF unction();

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 someOtherFuncti on()
{
Test(Derived::C lassId()).someF unction();
}
};

The error message I am getting with gcc now is:

error: cannot declare member function 'Test Derived::ClassI d()' 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
There was a slight mistake on my side: The error message of gcc is:

error: cannot declare member function `Derived::Class Id' within `use'

without Test as a return type...

Apr 21 '06 #9

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::Clas sId());

// This crashes
Test(Derived::C lassId());

return 0;

}

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

The line

Test(Derived::C lassId());

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::ClassI d()' does not
match any
in class `Derived'
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassI d()
test.cpp:24: error: `Test Derived::ClassI d()' and `static unsigned int
Derived::ClassI d()' cannot be overloaded
test.cpp:24: error: prototype for `Test Derived::ClassI d()' does not
match any
in class `Derived'
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassI d()
test.cpp:24: error: `Test Derived::ClassI d()' and `static unsigned int
Derived::ClassI d()' cannot be overloaded
test.cpp:24: error: declaration of `Test Derived::ClassI d()' 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
9377
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1, 2, func);
30
3497
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
2
3640
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
6
1712
by: JKop | last post by:
unsigned int CheesePlain(void) { unsigned int const chalk = 42; return chalk; } unsigned int& CheeseRef(void) {
32
69698
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: std::vector<int> fun(){ //build the vector v; return v; }
3
13480
by: silentcoast | last post by:
Alright im not sure why but im getting a "member function redeclaration not allowed" error when I to compile this simple program. main.cpp #include "main.h" CNetwork net; int main() { int on = 0;
11
1605
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
struct foo { int i; }; int bar(foo& f) { return f.i++; } int main() { bar(foo());
28
2857
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't apply. Can somebody tell me why? Thanks, Jess
10
4118
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not allowed to access the protected data members of the base object. This surprises me. Can someone explain why this is? I suspect there is a good reason and I am just having a slow day to not come up with it myself. Bob
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.