c++ - 错误: ambiguous overload for 'operator<<'

标签 c++ operator-overloading

当我尝试构建项目时遇到此错误:

Angle.cpp:466: error: ambiguous overload for 'operator<<' in '(+out)-
>std::basic_ostream<_Elem, _Traits>::operator<< [with _Elem = char, 
_Traits = std::char_traits<char>](((const Angle*)this)->Angle::getDegrees())
<< "\37777777660"'

我对此进行了一些研究,看起来我有一个错误的方法 header ,但我是 cpp 新手,不知道如何修复此错误。

这是 .h:

#include "OutputOps.h"
// End user added include file section

#include <vxWorks.h>
#include <ostream>
class Angle {

public:
// Default constructor/destructor
~Angle();

// User-defined methods
//
// Default Constructor sets Angle to 0.
Angle();
...
// Returns the value of this Angle in degrees.
double getDegrees() const;
....
// Prints the angle to the output stream as "x°" in degrees
void output(std::ostream& out) const;

...
private:
...
double radians;
static const double DEGREES_PER_RADIAN /* = 180.0 / PI */;    
};

#endif // ANGLE_H

方法如下:

#include "MathUtility.h"
#include <cmath>
// End user added include file section

#ifndef Angle_H
#include "Angle.h"
#endif

//
// Prints the angle to the output stream as "x°" in degrees
void Angle::output(std::ostream& out) const
{
    out << getDegrees() << "°";
}
//
// Returns the value of this Angle in degrees.
double Angle::getDegrees() const
{
return radians * DEGREES_PER_RADIAN;
}

最佳答案

我的猜测是问题出在度数符号上,它不是一个 ascii 字符。

尝试一下:

wcout <<  getDegrees() << L"\u00B0";

关于c++ - 错误: ambiguous overload for 'operator<<' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11522865/

相关文章:

c++ - 数组下标运算符重载

c++ - 没有名为 'argument_type' 的类型

c++ - C++ 中的原型(prototype)

c++ - 我可以在 c++11 中的 std::deque 上使用 std::max_element() 吗?

c++ - 读取/proc/pid/status的正确方式

c++ - 模板中的隐式转换和编译器强制

.net - F# 和 op_GreaterThan

c++ - 打印对象的映射,其中另一个对象作为键

c++ - 运算符重载优先级

c++ - 如何处理 float 溢出?