初始化时无法转换为`int' [英] Cannot convert to `int' in initialization

查看:367
本文介绍了初始化时无法转换为`int'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序有什么问题?


--- foo.cpp ---

struct Foo {};

int main()

{

Foo foo1,foo2;

for(int i = 0,foo1 = foo2 ;;); //第5行

返回0;

}

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


---汇编---


// GNU gcc / gpp 3.4.1


$ gpp foo.cpp

foo.cpp:在函数`int main()''中:

foo.cpp:5:错误:无法将`Foo'转换为初始化中的'int''


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


Alex Vinokur

电子邮件:alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

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

推荐答案

gpp foo.cpp

foo.cpp:在函数`int main()''中:

foo.cpp:5:错误:初始化时无法将`Foo''转换为'int''


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


Alex Vinokur

电子邮件:啤酒x DOT vinokur AT gmail DOT com
http://mathforum.org /library/view/10978.html
http:// sourceforge。 net / users / alexvn
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


* Alex Vinokur:
* Alex Vinokur:
以下程序有什么问题?
--- foo.cpp ---
struct Foo {};
int main()
{
Foo foo1,foo2;
for(int i = 0,foo1 = foo2; ; ); //第5行
返回0;
}
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;
}




很多错,但它没有_compile_的原因是赋值

''foo1 = foo2''产生一个需要条件的Foo值 -

条件必须是一个可以隐式转换为bool的值。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕东西?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?



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?




" Alex Vinokur" <人**** @ big-foot.com>在消息中写道

news:30 ************* @ uni-berlin.de ...

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:30*************@uni-berlin.de...
以下程序有什么问题?

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


声明


int i = 0,foo1 = foo2


声明两个int类型的变量:i和foo1。第一个用值$ 0初始化为
。第二个应该用值foo2初始化,但是

没有从这个值转换为int。

返回0;
}
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;
}




为了好玩,试试:


#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";

休息;

};

返回0;

}


Jonathan



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


这篇关于初始化时无法转换为`int'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆