"std::cin >>"中"运算符>>"的不明确重载
Ambiguous overload for ‘operator>>’ in ‘std::cin >>'
我在尝试获取一些代码以进行编译的赋值时遇到问题。我看过类似的问题(即使是那些看起来完全相同的任务),但没有一个解决方案奏效。
我的头文件:"复杂.h":
#ifndef COMPLEX_H_INCLUDED
#define COMPLEX_H_INCLUDED
#include <iostream>
#include <cstdlib>
using namespace std;
class Complex
{
public:
// Consructors:
Complex();
Complex(double);
Complex(double, double);
// Operational Overloads:
const bool operator == (const Complex&);
const Complex operator + (const Complex&);
const Complex operator + (const double&);
const Complex operator - (const Complex&);
const Complex operator - (const double&);
const Complex operator * (const Complex&);
const Complex operator * (const double&);
friend ostream& operator << (ostream&, const Complex&);
friend istream& operator >> (istream&, Complex&);
private:
double real;
double imaginary;
};
#endif // COMPLEX_H_INCLUDED
我的实现代码"复杂.cpp":
#include "complex.h"
#include <iostream>
using namespace std;
/****************************************************************************
* Constructor () *
****************************************************************************/
inline Complex::Complex(){
real = 0;
imaginary = 0;
}
/****************************************************************************
* Constructor (double) *
****************************************************************************/
inline Complex::Complex(double realPart)
:real(realPart)
{
imaginary = 0;
}
/****************************************************************************
* Constructor (double, double) *
****************************************************************************/
inline Complex::Complex(double realPart, double imaginaryPart)
:real(realPart), imaginary(imaginaryPart)
{}
/****************************************************************************
* Operator '==' -- Compares two Complex objects *
* Returns: *
* -- True if their real and imaginary values are equal *
* -- False otherwise *
****************************************************************************/
inline const bool Complex::operator == (const Complex& rhs)
{
return ((this->real == rhs.real) && (this->imaginary == rhs.imaginary));
}
/****************************************************************************
* Operator '+' -- Adds two Complex objects *
* Returns: *
* -- The new Complex object *
****************************************************************************/
inline const Complex Complex::operator + (const Complex& rhs)
{
return (Complex((this->real + rhs.real), (this->imaginary + rhs.imaginary)));
}
/****************************************************************************
* Operator '+' -- Adds a real number to the Complex object *
* Returns: *
* -- The new Complex object *
****************************************************************************/
inline const Complex Complex::operator+(const double& rhs)
{
return Complex((this->real + rhs), this->imaginary);
}
/****************************************************************************
* Operator '-' -- Subtracts the number of another Complex object *
* Returns: *
* -- The new Complex object *
****************************************************************************/
inline const Complex Complex::operator - (const Complex& rhs)
{
return (Complex((this->real - rhs.real), (this->imaginary - rhs.imaginary)));
}
/****************************************************************************
* Operator '-' -- Subtracts a real number from the Complex object *
* Returns: *
* -- The new Complex object *
****************************************************************************/
inline const Complex Complex::operator-(const double& rhs)
{
return Complex((this->real - rhs), this->imaginary);
}
/****************************************************************************
* Operator '*' -- Multiplies together two Complex objects *
* Returns: *
* -- The new Complex object *
****************************************************************************/
inline const Complex Complex::operator * (const Complex& rhs)
{
return (Complex((this->real * rhs.real) - (this->imaginary * rhs.imaginary), (this->real * rhs.imaginary) + (this->imaginary * rhs.real)));
}
/****************************************************************************
* Operator '*' -- Mutltiplies the Complex object by a real number *
* Returns: *
* -- The new Complex object *
****************************************************************************/
inline const Complex Complex::operator * (const double& rhs)
{
return Complex((this->real * rhs), (this->imaginary * rhs));
}
/****************************************************************************
* Operator '<<' -- Outputs the Complex object in a comprehensive format *
* Returns: *
* -- A reference to the ostream *
****************************************************************************/
ostream& operator <<(ostream& out, const Complex& comp)
{
if(comp.real != 0)
{
out << comp.real;
if(comp.imaginary > 0)
out << " + ";
else if (comp.imaginary < 0)
out << " - ";
}
if(comp.imaginary != 0)
out << comp.imaginary << "i";
return out;
}
/****************************************************************************
* Operator '>>' -- Takes in a Complex object from the user *
* Returns: *
* -- A reference to the istream *
****************************************************************************/
istream& operator >>(istream& in, Complex& comp)
{
char sign; //Used to store input when looking for mathematical operators
bool neg = false; //Stores whether or not any input values are <0
// Checks for a negative value for the real
sign = std::cin.peek();
if(sign == '-'){
neg = true;
std::cin.ignore(1,'-');
}
in >> comp.real;
//Negates the real value if necessary
if(neg){
comp.real = -comp.real;
neg = false;
}
//Looks for the + or - operator, accounting for possible variations in whitespacing and hazardous input.
do{
in >> sign;
}while (sign == ' ');
if (sign == '-')
neg = true;
else if (sign != '+'){
cout << "You did not properly format the input of a complex number. nPlease use the format: 'a+bi' where 'a' is the real number, and 'b' is the imaginary number." << endl;
cout << "The program is currently shutting down.";
exit(EXIT_FAILURE);
}
sign = std::cin.peek();
while(sign == ' ')
in >> sign;
in >> comp.imaginary;
if(neg)
comp.imaginary = -comp.imaginary;
}
和实现复杂.cpp的最后一个类:
#include <iostream>
#include "complex.cpp"
using namespace std;
int main()
{
Complex aComplex();
cout << "Please enter a complex number, in the form of 'a+bi', where 'a' is a real number and 'b' the imaginary number." << endl;
cin >> aComplex;
cout << "You entered the value: " << aComplex << endl;
return 0;
}
当我尝试构建代码时,出现错误:
.../Complex/main.cpp|12|error: ambiguous overload for ‘operator>>’ in ‘std::cin >> aComplex'`
有人有什么想法吗?(我正在使用Code::Blocks 12.11并使用Ubuntu 13.04,如果有帮助的话。
问题是aComplex
被声明为一个函数!您可能打算编写以下内容之一:
Complex aComplex;
Complex bComplex = Complex();
Complex cComplex{};
寻找"最烦人的解析"来解释为什么你的aComplex
是一个函数声明。
Complex aComplex();
这不调用Complex
的构造函数,它创建一个函数的人,接受零参数并返回一个类型为Complex
的对象。如果要实例化对象,则应删除空括号:
Complex aComplex;
相关文章:
- Visual C++(VS2017)中用户定义的转换不明确
- 重载类方法的不明确调用
- 为函数定义符号不明确的指针参数
- 父类的私有函数会导致对具有相同名称和相似参数的子类中的公共函数的不明确调用
- 在 C++17 中的命名空间和子命名空间中重载运算符是不明确的
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 对重载函数find_first_not_of的不明确调用
- 不明确的成员模板查找
- gcc出现不明确的模板实例化错误
- 调用'Node'构造函数是不明确的
- 如何解决不明确的运算符过载问题?
- 使用 nullptr 调用重载方法是不明确的
- "fpclassify":对重载函数的不明确调用
- 对"列表"的引用不明确,包括头文件
- 删除全局隐式函数 - 避免使用不明确的运算符
- 当接收到不明确的规范时,表示图的邻接列表的数据结构
- "byte":使用Crypto++和Windows SDK时出现不明确的符号错误
- 使用 std::iterator_traits<> 时编译时错误不明确
- 不明确的函数模板
- Vulkan中的描述符集计数不明确