所以我正在为一个学校项目开发一个 C++ 程序。
我使用了一堆条件运算符(我喜欢简写),它在 C Shell 上运行得很好(这是链接。程序在那里运行得很好... http://cpp.sh/7fsj ),但它无法在 Visual Studio 上运行C++,我很好奇我的代码中是否存在某种错误......
我的程序是:
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter an angle and I will tell you which quadrant it is in.\n\n\n";
double angle;
cin >> angle;
cout << endl;
bool first, second, third, fourth, xaxis, yaxis, nxaxis, nyaxis;
if (angle < 0 || angle > 360)
cout << "Invalid input.";
else
{
(angle > 0 && angle < 90 ? first = true : first = false);
(first == true ? cout << "The angle you entered is in the first quadrant.\n\n" : cout << "");
(angle > 90 && angle < 180 ? second = true : second = false);
(second == true ? cout << "The angle you entered is in the second quadrant.\n\n" : cout << "");
(angle > 180 && angle < 270 ? third = true : third = false);
(third == true ? cout << "The angle you entered is in the third quadrant.\n\n" : cout << "");
(angle > 270 && angle < 360 ? fourth = true : fourth = false);
(fourth == true ? cout << "The angle you entered is in the fourth quadrant.\n\n" : cout << "");
(angle == 0 || angle == 360 ? nxaxis = true : nxaxis = false);
(angle == 180 ? xaxis = true : xaxis = false);
(nxaxis == true ? cout << "This angle lies on the negative portion of the x axis.\n\n" : "");
(xaxis == true ? cout << "This angle lies on the positive portion of the x axis.\n\n" : "");
(angle == 90 ? yaxis = true : yaxis = false);
(angle == 270 ? nyaxis = true : nyaxis = false);
(yaxis == true ? cout << "This angle lies on the positive portion of the y axis.\n\n" : "");
(nyaxis == true ? cout << "This angle lies on the negative portion of the y axis.\n\n" : "");
}
system("pause");
return 0;
}
它在那里运行,但不在 VS 中运行
我找不到任何逻辑错误或错误原因,但它一直这样说
"1>------ Build started: Project: lab-projects, Configuration: Debug Win32 ------
1> project.cpp
1>c:\users\wishi\documents\visual studio 2010\projects\lab-projects\lab-projects\project.cpp(179): error C2446: ':' : no conversion from 'const char *' to 'std::basic_ostream<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\wishi\documents\visual studio 2010\projects\lab-projects\lab-projects\project.cpp(180): error C2446: ':' : no conversion from 'const char *' to 'std::basic_ostream<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\wishi\documents\visual studio 2010\projects\lab-projects\lab-projects\project.cpp(183): error C2446: ':' : no conversion from 'const char *' to 'std::basic_ostream<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\wishi\documents\visual studio 2010\projects\lab-projects\lab-projects\project.cpp(184): error C2446: ':' : no conversion from 'const char *' to 'std::basic_ostream<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========="
这是一个初级编程类(class),所以我不知道这意味着什么。 有人可以帮忙吗?
最佳答案
您滥用了三元运算符,通常且更易读的使用方式如下:
xaxis = angle == 180 ? true : false;
您将其用作完整的控制结构,而不是 if
s。
现在您遇到的错误是有道理的。关于三元运算符的规则之一规定 ?
之后的两个表达式必须具有相同的类型。以这一行为例:
(nxaxis == true ? cout << "This angle lies on the negative portion of the x axis.\n\n" : "");
第一个表达式 ( cout << "..."
) 是 ostream
第二个( ""
)是 const char *
。这就是错误消息试图告诉您的内容,它正在尝试将第二个表达式转换为 ostream
以匹配第一个的类型。
要解决这个问题,您可以像前面几行中那样编写表达式,即使它很难看:
(nxaxis == true ? cout << "This angle lies on the negative portion of the x axis.\n\n" : cout << "");
或者你可以这样写:
cout << (nxaxis ? "This angle ..." : "");
或者更好:
if (nxaxis)
{
cout << "This angle ...";
}
关于c++ - C Shell(cpp.sh,基于浏览器的编译器)正在运行 Visual Studio 不会运行的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39783701/