C++ 我的类中的相等运算符(=)不起作用
我一直在跟随一些cpp练习来学习cpp,我遇到了一个问题 我已经创建了一个名为“FixedPoint2”的类来实现一个固定点数为2位小数。我已经包括了头文件,其中包括下面的所有函数 我正在努力解决的是,当我尝试测试等式运算符时,我总是得到一个错误的答案。换言之,发生以下情况:C++ 我的类中的相等运算符(=)不起作用,c++,class,operator-overloading,equality-operator,C++,Class,Operator Overloading,Equality Operator,我一直在跟随一些cpp练习来学习cpp,我遇到了一个问题 我已经创建了一个名为“FixedPoint2”的类来实现一个固定点数为2位小数。我已经包括了头文件,其中包括下面的所有函数 我正在努力解决的是,当我尝试测试等式运算符时,我总是得到一个错误的答案。换言之,发生以下情况: cout << (FixedPoint2(1.0)==FixedPoint2(1.0)) << endl; //returns true as expected cout << (Fix
cout << (FixedPoint2(1.0)==FixedPoint2(1.0)) << endl; //returns true as expected
cout << (FixedPoint2(1.2)==FixedPoint2(1.2)) << endl; //returns false
cout << FixedPoint2(1.2) << "\t" << FixedPoint2(1.2) << endl; returns 1.2 1.2
cout编译器不会自动生成operator==
,因为出错的几率比正确的几率大得多。让我们举一个简单的例子:动态字符串。编译器会生成一个字符一个字符比较的代码,对吗?什么时候停?现在,编译器需要更多地了解程序员使用字符串的意图,并且编译器不需要心灵感应接口的额外复杂性
最好有一个一致的规则,不,并强制明确定义要比较的内容,而不是一个由人们假设他们得到了他们想要的东西而产生的垃圾代码雷区。关于该主题的详细讨论如下:
编译器四处寻找满足比较的方法。它没有找到直接的运算符==
,但它确实找到了运算符double
,并且可以使用运算符double
进行比较。但有时他们不能:
也就是说我无法复制OP的结果。我希望在完全相同的输入上执行完全相同的公式,以得到完全相同的结果,即使结果是1.199999。。。而不是1.2
虽然我无法再现,但OP最好还是实现定点操作符==
,因为定点数字没有不精确性。不动点将等于或不等于,没有ifs and或but,所以“不会出错!”此外,这个操作符应该很容易编写。类似于return(rhs.m_digi==lhs.m_digi)和&(rhs.m_deci==lhs.m_deci)代码>解决此类问题的正确工具是调试器。在询问堆栈溢出之前,应该逐行检查代码。如需更多帮助,请阅读。至少,您应该[编辑]您的问题,以包括一个重现您的问题的示例,以及您在调试器中所做的观察。您的运算符==
重载在哪里?@c650我认为他在运算符double()上过得去代码>转换和比较双精度,这可能使它成为一个例子,但我们也没有定义固定点
,所以我知道什么?谢谢。下次我会遵守协议的。今天我根据一位朋友的建议解决了这个问题。问题是==运算符没有重载。我不知道为什么这会导致错误。任何见解?@ A.在C++中,没有默认的<代码>运算符==/COD>
if (FixedPoint2(4.5)==FixedPoint2(4.5))
cout << "Don't post to stackoverflow"; //This doesn't print
using namespace std;
class FixedPoint2
{
private:
int16_t m_digi; //chosen because I want the range
int8_t m_deci; //chosen to optimise memory
public:
FixedPoint2(int16_t digi = 0, int8_t deci = 0):m_digi{digi}, m_deci{deci}
{
assert(!(deci>127 || deci<-127)); //to prevent overflows
if(deci<-100 || deci>100) //just in case some moron (me) does some weird decimal calculations
{
m_digi+=(static_cast<int16_t>(deci)/100);
m_deci+=(-100);
}
}
FixedPoint2(double mydouble)
{
if (mydouble>=0) //The if-else floors the absolute value of the integer base
{
m_digi=static_cast<int16_t>(floor(mydouble));
}
else
{
m_digi=static_cast<int16_t>(floor(mydouble)+1);
}
m_deci=static_cast<int8_t>(round(100*(mydouble-m_digi))); //This rounds off the decimal to two digits
};
operator double();
friend ostream& operator<<(ostream &out, const FixedPoint2 &f1);
friend istream& operator>>(istream &in, FixedPoint2 &f1);
friend FixedPoint2 operator+(const FixedPoint2 &f1, const FixedPoint2 &f2);
};
FixedPoint2::operator double()
{
double base= static_cast<double>(m_digi);
double decimal= static_cast<double>(m_deci);
return base+0.01*decimal;
}
ostream& operator<<(ostream &out, const FixedPoint2 &f1)
{
FixedPoint2 a=f1;
out << double(a); //this is an easy work around to handling the period placement for the fixed point number
return out;
}
istream& operator>>(istream &in, FixedPoint2 &f1)
{
double placeholder;
in>>placeholder;
f1=FixedPoint2(placeholder);
return in;
}
FixedPoint2 operator+(const FixedPoint2 &f1, const FixedPoint2 &f2)
{
return FixedPoint2(f1.m_digi+f2.m_digi, f1.m_deci+f2.m_deci);
}