错误:'operator<<'的重载不明确

error: ambiguous overload for 'operator<<'

本文关键字:lt 重载 不明确 operator 错误      更新时间:2023-10-16

当我试图构建我的项目时,我遇到了这个错误:

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"'

我对它做了一些研究,看起来我有一个错误的方法头,但我是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";