Strange gcc compiler error?

J

J Cook

Can anyone explain this error? It seems as if the compiler is using
the name of the first parameter as the temporary object name?

class Foo
{
Foo(int s) {}

};

void foo()
{
int test = 4;
Foo(test); // create a temporary. Thinks it is named "test" ??
}

int main()
{
foo();
}

# In function 'void foo()'
# error: conflicting declaration 'Foo test'
# 'test' has a previous declaration as 'int test'

gcc 4.1.2

Thanks in advance,
JC
 
V

Victor Bazarov

Can anyone explain this error? It seems as if the compiler is using
the name of the first parameter as the temporary object name?

class Foo
{
Foo(int s) {}

};

void foo()
{
int test = 4;
Foo(test); // create a temporary. Thinks it is named "test" ??
}

int main()
{
foo();
}

# In function 'void foo()'
# error: conflicting declaration 'Foo test'
# 'test' has a previous declaration as 'int test'

gcc 4.1.2

In order to verify you could use www.comeaucomputing.com/tryitout (they
compile but don't link), their compiler is very Standard-compliant.

As to your "error" since 'Foo' is the name of the type, the compiler can
(and will) interpret

Foo(test);

as a declaration of a variable 'test' of type 'Foo', thus trying to
redeclare 'test' with another type. To avoid that add parentheses

Foo((test));

V
 
J

J Cook

On 5/3/2011 12:51 PM, J Cook wrote:
\> In order to verify you could usewww.comeaucomputing.com/
tryitout(they
compile but don't link), their compiler is very Standard-compliant.

As to your "error" since 'Foo' is the name of the type, the compiler can
(and will) interpret

     Foo(test);

as a declaration of a variable 'test' of type 'Foo', thus trying to
redeclare 'test' with another type.  To avoid that add parentheses

     Foo((test));

You are right about the semantic confusion. I had thought of the
double parenthesis would fix it also, but it does not seem to make any
difference. I really want to create an unnamed temporary (a common
task), and can't seem to find a way to hint the parser that this
should be a Constructor call with one parameter.

Thanks,
--JC
 
K

Kai-Uwe Bux

J said:
Can anyone explain this error? It seems as if the compiler is using
the name of the first parameter as the temporary object name?

class Foo
{
Foo(int s) {}

};

void foo()
{
int test = 4;
Foo(test); // create a temporary. Thinks it is named "test" ??
}

int main()
{
foo();
}

# In function 'void foo()'
# error: conflicting declaration 'Foo test'
# 'test' has a previous declaration as 'int test'
[...]

The compiler treats the marked line not as creating a temporary but as a
variable declaration of the variable test. At least, that is what the error
message says.

What one would expect, is a message: "constructor Foo(int) is private and
cannot be used in this context.". However, after discarding the private
constructor, the compiler goes on and tries the default constructor. That's
why you get a redefinition error message. The compiler does this, because
clause [8.3/6] says that one way of reading

Foo(test);

is:

Foo test;


Best,

Kai-Uwe Bux
 
Ö

Öö Tiib

Or, another way is to cast:

     (Foo)test;

Still does not work since Foo constructor is private in example code.
If it is made public and explicit (that it usually should be with just
one parameter) then such monster works:

(void)Foo(test);
 
J

James Kanze

Still does not work since Foo constructor is private in example code.
If it is made public and explicit (that it usually should be with just
one parameter) then such monster works:

(void)Foo(test);

Or simply putting the entire expression in parentheses:

(Foo( test ));

(But it sure looks funny:).)
 

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

Latest Threads

Top