#include <iostream>
using namespace std;
int main()
{
int test = 0;
cout << (test ? "A String" : 0) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
三元?:运算符要求两个输出操作数要么是相同的数据类型,要么至少可以转换为公共数据类型.A char*不能隐式转换为a int,但0文字可以隐式转换为a char*.
试试这个:
#include <iostream>
int main()
{
int test = 0;
std::cout << ((test) ? "A String" : "") << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
要么:
#include <iostream>
int main()
{
int test = 0;
std::cout << ((test) ? "A String" : (char*)0) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果你想0在test0 时实际输出,那么你必须这样做:
#include <iostream>
int main()
{
int test = 0;
std::cout << ((test) ? "A String" : "0") << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
否则,请删除?:运算符,因为您无法使用它来混合不同的不兼容数据类型以进行输出:
#include <iostream>
int main()
{
int test = 0;
if (test)
std::cout << "A String";
else
std::cout << 0;
std::cout << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
121 次 |
| 最近记录: |