下午好🌅🌅🌅
本答案参考ChatGPT-3.5
问题分析:
在C++中,"cout"是输出流对象,它在命名空间std中有两种定义方式:一种是在头文件iostream中定义的std::cout(标准输出流),另一种是在头文件iosfwd中定义的std::basic_ostream< _CharT, _Traits >的类模板实例化对象。
在使用using namespace std;指令后,编译器会在前述两个定义中查找"cout"的符号,由于这两个定义都存在,就会引发编译错误"ambiguous symbol"。
解决方案:
- 不使用using namespace std;,而是使用std::cout来指代cout输出流对象:
std::cout << "please input a,b,c:";
- 在代码中使用using声明,而不是使用using namespace指令,这样只引入所需的符号,避免引起冲突:
using std::cout;
using std::cin;
- 将std命名空间改为具体的标识符:
std::cout << "please input a,b,c:";
修改后的代码示例如下:
#include <iostream>
#include <cmath>
int main()
{
float a, b, c, disc;
std::cout << "please input a,b,c:";
std::cin >> a >> b >> c;
if (a == 0)
std::cerr << "a is equal to zero, error!" << std::endl;
else if ((disc = b * b - 4 * a * c) < 0)
std::cerr << "disc=b*b-4*a*c<0" << std::endl;
else {
std::cout << "x1=" << (-b + std::sqrt(disc)) / (2 * a) << std::endl;
std::cout << "x2=" << (-b - std::sqrt(disc)) / (2 * a) << std::endl;
}
return 0;
}