Cannot convert to `int' in initialization

A

Alex Vinokur

What is wrong in program below?

--- foo.cpp ---
struct Foo {};
int main ()
{
Foo foo1, foo2;
for (int i = 0, foo1 = foo2; ; ); // Line#5
return 0;
}
---------------

--- Compilation ---

// GNU gcc/gpp 3.4.1

$ gpp foo.cpp
foo.cpp: In function `int main()':
foo.cpp:5: error: cannot convert `Foo' to `int' in initialization

-------------------

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
 
A

Alf P. Steinbach

* Alex Vinokur:
What is wrong in program below?

--- foo.cpp ---
struct Foo {};
int main ()
{
Foo foo1, foo2;
for (int i = 0, foo1 = foo2; ; ); // Line#5
return 0;
}

Much wrong, but the reason it doesn't _compile_ is that the assignment
'foo1 = foo2' produces a Foo value where a condition is required -- a
condition must be a value that can be implicitly converted to bool.
 
A

Alex Vinokur

Alf P. Steinbach said:
* Alex Vinokur:

Much wrong, but the reason it doesn't _compile_ is that the assignment
'foo1 = foo2' produces a Foo value where a condition is required -- a
condition must be a value that can be implicitly converted to bool.
[snip]

for (int i = 0, foo1 = foo2; ; );
or
for (int i = 0, foo1 = foo2; i < 100; i++);

Why is a condition required for 'foo1 = foo2'?
 
A

Alex Vinokur

Jonathan Turkanis said:
The declaration

int i = 0, foo1 = foo2

declares two variables of type int: i and foo1. The first is initialized with
the value 0. The second is supposed to be initialized with the value foo2, but
there is not conversion from this value to an int.

Sorry & Thanks.

Of courcs, now it is OK.

struct Foo {};
int main ()
{
Foo foo1, foo2;
int i;
for (i = 0, foo1 = foo2; ; );
return 0;
}
[snip]
 
A

Alex Vinokur

Jonathan Turkanis said:
For fun, try:

#include <iostream>
#include <typeinfo>

struct Foo {
operator int() const { return 5; }
};

int main ()
{
Foo foo1, foo2;
for (int i = 0, foo1 = foo2; ; ) {
std::cout << "typeof foo1 = " << typeid(foo1).name() << "\n";
std::cout << "foo1 = " << foo1 << "\n";
break;
};
return 0;
}
[snip]

Indeed, very nice.
 
J

Jonathan Turkanis

Alex Vinokur said:
What is wrong in program below?

--- foo.cpp ---
struct Foo {};
int main ()
{
Foo foo1, foo2;
for (int i = 0, foo1 = foo2; ; ); // Line#5

The declaration

int i = 0, foo1 = foo2

declares two variables of type int: i and foo1. The first is initialized with
the value 0. The second is supposed to be initialized with the value foo2, but
there is not conversion from this value to an int.
return 0;
}

For fun, try:

#include <iostream>
#include <typeinfo>

struct Foo {
operator int() const { return 5; }
};

int main ()
{
Foo foo1, foo2;
for (int i = 0, foo1 = foo2; ; ) {
std::cout << "typeof foo1 = " << typeid(foo1).name() << "\n";
std::cout << "foo1 = " << foo1 << "\n";
break;
};
return 0;
}

Jonathan
 
J

Jonathan Turkanis

Alf P. Steinbach said:
* Alex Vinokur:

Much wrong, but the reason it doesn't _compile_ is that the assignment
'foo1 = foo2' produces a Foo value where a condition is required -- a
condition must be a value that can be implicitly converted to bool.

I think you missed the fact that 0 and foo1 are separated by a comma.

Jonathan
 
A

Andrew Koenig

Of courcs, now it is OK.

struct Foo {};
int main ()
{
Foo foo1, foo2;
int i;
for (i = 0, foo1 = foo2; ; );
return 0;
}

You could also have written it this way if you wanted to confuse people:

Foo foo1, foo2;
for (int i = foo1 = foo2, 0; ;) ;

PS: Don't try this for real, but do understand why it works.
 
A

Alex Vinokur

Andrew Koenig said:
You could also have written it this way if you wanted to confuse people:

Foo foo1, foo2;
for (int i = foo1 = foo2, 0; ;) ;

PS: Don't try this for real, but do understand why it works.

GNU gpp 3.4.1 doesn't compile that.

--- foo.cpp ---
struct Foo {};
int main ()
{
Foo foo1, foo2;
for (int i = foo1 = foo2, 0; ;) ; // Line#5
}
---------------

--- Compilation ---
$ gpp foo.cpp
foo.cpp: In function `int main()':
for.cpp:5: error: cannot convert `Foo' to `int' in initialization
for.cpp:5: error: expected unqualified-id before numeric constant
for.cpp:5: error: expected `,' or `;' before numeric constant
 
R

Rob Williscroft

Alex Vinokur wrote in in comp.lang.c++:
GNU gpp 3.4.1 doesn't compile that.

--- foo.cpp ---
struct Foo {};
int main ()
{
Foo foo1, foo2;
for (int i = foo1 = foo2, 0; ;) ; // Line#5
}
---------------

--- Compilation ---
$ gpp foo.cpp
foo.cpp: In function `int main()':
for.cpp:5: error: cannot convert `Foo' to `int' in initialization
for.cpp:5: error: expected unqualified-id before numeric constant
for.cpp:5: error: expected `,' or `;' before numeric constant

Well spotted, this:

struct Foo {};
int main ()
{
Foo foo1, foo2;
for (int i = (foo1 = foo2, 0); i < 10; )
{
++i;
}
}

does compile.

There are two issues, the first is that the comma in Andrews version
*isn't* the comma *operator* but a comma *seperator*, part of a
init-declarator-list (8/1).

Secondly using the comma *operator* doesent help, as:

int i;
for (i = foo1 = foo2, 0; i < 10; )
{
++i;
}

doesn't compile either, here the comma is the comma *operator* but the
assignment operator has a higher precidence than the comma operator
so: i = foo1 = foo2, 0; is equivilant to: (i = foo1 = foo2), 0;

Rob.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,083
Latest member
SylviaHarr

Latest Threads

Top