gpt4 book ai didi

c++ - "undefined reference to"C++运算符重载

转载 作者:搜寻专家 更新时间:2023-10-31 01:30:35 24 4
gpt4 key购买 nike

我想创建类Q(有理数),它具有以下功能:

1)print(): 以:numerator/denominator 的形式打印有理数

2)cancel():执行取消,如果可能的话:(3/9 = 1/3)

3)value():有理数的取值为double类型;

4)运算符重载加法、减法、除法、乘法。

这是我的代码:

#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
using namespace std;


class Q{
private:
int num;
int denom;

public:
Q();
Q(int num,int denom);
void print();
Q cancel();
void value();
friend Q operator*(Q lhs,Q rhs);
friend Q operator/(Q lhs,Q rhs);
friend Q operator+(Q lhs,Q rhs);
friend Q operator-(Q lhs,Q rhs);
};

Q::Q(int numerator,int denominator){
num = numerator;
denom = denominator;
}


void Q::print(){
cout << to_string(num) + "/" + to_string(denom);
}

Q Q::cancel(){
vector<int> canc_val = {2,3,5,7};
while(true){
if(num % canc_val[0] == 0 && denom % canc_val[0] == 0){
num /= canc_val[0];
denom /= canc_val[0];
} else if(num % canc_val[1] == 0 && denom % canc_val[1] == 0){
num /= canc_val[1];
denom /= canc_val[1];
} else if(num % canc_val[2] == 0 && denom % canc_val[2] == 0){
num /= canc_val[2];
denom /= canc_val[2];
}else if(num % canc_val[3] == 0 && denom % canc_val[3] == 0){
num /= canc_val[3];
denom /= canc_val[3];
}else{
break;
}
}
return Q(num,denom);
}

void Q::value(){
double res;
res = num/denom;
cout << res;
}

Q operator*(Q lhs,Q rhs){
Q temp;
temp.num = lhs.num * rhs.num;
temp.denom = lhs.denom * rhs.denom;
return temp;
}

Q operator/(Q lhs,Q rhs){
Q temp;
temp.num = lhs.num * rhs.denom;
temp.denom = lhs.denom * rhs.num;
return temp;
}

Q operator+(Q lhs,Q rhs){
Q temp;
temp.num = lhs.num * rhs.denom + rhs.num*lhs.denom;
temp.denom = lhs.denom * rhs.denom;
return temp;
}

Q operator-(Q lhs,Q rhs){
Q temp;
temp.num = lhs.num * rhs.denom - rhs.num*lhs.denom;
temp.denom = lhs.denom * rhs.denom;
return temp;
}

int main(){
Q number_1(3,7);
Q number_2(3,9);
Q number_3(15,125);

number_1.print();
cout << "\n";
number_2.print();
number_2.cancel();
cout << "\n";
number_2.print();
cout << "\n";
number_3.value();
cout << "\n";
number_3.cancel();
cout << "\n";
number_3.print();

Q number_4 = number_1 + number_2;
number_4.print();
number_4.cancel();
cout << "\n";
number_4.print();

Q number_5 = number_2 - number_3;
number_5.print();
number_5.cancel();
cout << "\n";
number_5.print();


Q number_6 = number_1 * number_1;
number_6.print();
number_6.cancel();
cout << "\n";
number_6.print();

Q number_7 = number_2 / number_3;
number_7.print();
number_7.cancel();
cout << "\n";
number_7.print();

}

但是编译器给我一个错误:

/tmp/cce4zPM4.o: In function `operator*(Q&, Q&)':
:(.text+0x170): undefined reference to `Q::Q()'
/tmp/cce4zPM4.o: In function `operator/(Q&, Q&)':
:(.text+0x1b0): undefined reference to `Q::Q()'
/tmp/cce4zPM4.o: In function `operator+(Q&, Q&)':
:(.text+0x1f0): undefined reference to `Q::Q()'
/tmp/cce4zPM4.o: In function `operator-(Q&, Q&)':
:(.text+0x230): undefined reference to `Q::Q()'
collect2: error: ld returned 1 exit status

我不知道如何修复它。你能帮帮我吗?

最佳答案

Q::Q() 没有定义,只有声明,但您必须提供定义,如下所示:

Q::Q() :
num(0),
denom(1)
{
}

不要在没有初始化列表的情况下定义它,因为这意味着您的数据成员具有不确定的值,最终导致未定义的行为。

关于c++ - "undefined reference to"C++运算符重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47620810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com