473,549 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13367
* 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).na me() << "\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******@kanga roologic.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******@kanga roologic.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).na me() << "\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******** ***********@bgt nsc05-news.ops.worldn et.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

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

Similar topics

1
3230
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 value to a time value' error. It's related to glibc returning an error to a pre-1970 date, I think. My question: /how/ do I go through the Python...
5
18296
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 (%) Status MY_eems 368.49219 16.034182 0 I made a complete backup of the database and transaction log and then executed this statement:...
8
5457
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 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I...
22
27851
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 = (( curRow == System.DBNull.Value ) ? SqlInt32.Null : (int)curRow ); The errors are (in turn):
1
3728
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... Books->Columns->Add("Volume",System::Type::GetType(S"System.Int32")); Then below I try to add an int value to it.
12
10061
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 it. here is the 4 errors. c:\C++\Ch15\Employee.h(29): error C2440: '=' : cannot convert from 'char ' to 'char '
6
30139
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
4460
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 give me back any result could you tell me whats wrong with that. here is the pixel grabber file: package digitalpen; import...
14
13453
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. This is what I have so far: #include <stdio.h> #include <stdlib.h> #include <string.h>
0
7734
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. ...
0
7979
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...
1
7497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6065
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...
1
5385
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...
0
3512
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...
0
3493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1960
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1074
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.