473,385 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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::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
11 11023
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
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::ClassId());

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::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
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

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

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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
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,...
30
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
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...
6
by: JKop | last post by:
unsigned int CheesePlain(void) { unsigned int const chalk = 42; return chalk; } unsigned int& CheeseRef(void) {
32
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: ...
3
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() {...
11
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
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...
10
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.