473,320 Members | 1,713 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,320 software developers and data experts.

Cannot convert to `int' in initialization

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
Jul 22 '05 #1
10 13312
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:30*************@uni-berlin.de...
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
Jul 22 '05 #3

"Alf P. Steinbach" <al***@start.no> wrote in message news:41*****************@news.individual.net...
* 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.

[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'?

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


Jul 22 '05 #4

"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net...
* 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.


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

Jonathan
Jul 22 '05 #5

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message news:30*************@uni-berlin.de...

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:30*************@uni-berlin.de...
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.


Sorry & Thanks.

Of courcs, now it is OK.

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

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

Jul 22 '05 #6
* Jonathan Turkanis:

"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net...
* 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.


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


Yes.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #7

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message news:30*************@uni-berlin.de...
[snip]
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.
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #8
"Alex Vinokur" <al****@big-foot.com> wrote in message
news:30*************@uni-berlin.de...
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.
Jul 22 '05 #9

"Andrew Koenig" <ar*@acm.org> wrote in message news:8J*******************@bgtnsc05-news.ops.worldnet.att.net...
"Alex Vinokur" <al****@big-foot.com> wrote in message
news:30*************@uni-berlin.de...
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.


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

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


Jul 22 '05 #10
Alex Vinokur wrote in news:30*************@uni-berlin.de in comp.lang.c++:
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
--------------------


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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #11

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

Similar topics

1
by: Sorisio, Chris | last post by:
Ladies and gentlemen, I've imported some data from a MySQL database into a Python dictionary. I'm attempting to tidy up the date fields, but I'm receiving a 'mx.DateTime.Error: cannot convert...
5
by: BashiraInTrouble | last post by:
Hi Friends, I have tried almost everything but I cant seem to shrink the transaction log. Executing DBCC SQLPERF(LOGSPACE) gives me this info: Database Log Size (MB) Log Space Used (%) ...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
22
by: Christoph Boget | last post by:
I am getting an error (a few among many) for the following lines of code: retval.BrokerName = (( curRow == System.DBNull.Value ) ? SqlString.Null : (string)curRow ); retval.BrokerGroupId = ((...
1
by: Gary | last post by:
I have a strange compile error. C2664 cannot convert parameter 2 from int to int... Earlier in my code I was setting up my dataset... I add an int field like so... ...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
6
by: John | last post by:
The following code: int test = 1; bool isTrue = (bool)test; results in a compiler error: Cannot convert type 'int' to 'bool' wtf, any ideas on how to work around this?
2
by: minouparhizkar | last post by:
hi could anyone helping me to finish this i have no idea how to implement the program in java im trying to grabbing the pixel from image and then convert it to the txt file .i did that but it didnt...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.