C++ 错误C2679:二进制'<<';:未找到接受类型为';的右操作数的运算符;RatNum';(或没有可接受的转换)

C++ 错误C2679:二进制'<<';:未找到接受类型为';的右操作数的运算符;RatNum';(或没有可接受的转换),c++,overloading,C++,Overloading,我在重载函数时遇到了一个问题您的函数有错误的类型: friend-ostream&operator您的好友声明 friend ostream& operator<<(ostream &output, const RatNum &resultObj); friend ostream&Operator在RatMath类中的确切含义是什么:创建成员函数static@NeilKirk只意味着重载实现函数位于名为RatMath的类中。如果运算符是用于RatNum,为什

我在重载函数时遇到了一个问题您的函数有错误的类型:


friend-ostream&operator您的好友声明

friend ostream& operator<<(ostream &output, const RatNum &resultObj);

friend ostream&Operator在RatMath类中
的确切含义是什么:
创建成员函数static@NeilKirk只意味着重载实现函数位于名为RatMath的类中。如果运算符是用于
RatNum
,为什么它是
RatMath
的朋友?啊,这就是问题所在。我把它当作一个普通函数,没有意识到它必须在对象类中。目前还不需要const声明,但我将查找它以备将来参考。虽然它应该是const,但假设提供的代码示例正确,这不是错误的原因。不起作用:“该对象具有与成员函数'RatNum::getTopNum'对象不兼容的类型限定符:const RatNum”@AngelaKay那么你的
getTopNum
函数必须是常量。哦,它太简单了@AngelaKay-您必须将函数
ostream&operator放置在问题所在的位置,重载根本不在对象类中。谢谢
ostream& operator<<(ostream &output, RatNum &resultNum)
{
    int topNum = resultNum.getTopNum();
    int botNum = resultNum.getBotNum();

    output << topNum << "/" << botNum;
    return output;
}
RatNum testObj = RatNum(1, 3);
cout << testObj;
friend ostream& operator<<(ostream &output, const RatNum &resultObj);