这是我得到 Linker error undefined reference to RationalNumber::RationalNumber(int, int) 的错误
我尝试使用 Visual Studio 并且错误说,找不到指定的文件rationalnumber.exe。
头文件
#ifndef __Assignment_2Q1__RationalNumber__
#define __Assignment_2Q1__RationalNumber__
#include <iostream>
using namespace std;
class RationalNumber{
public:
RationalNumber(int=0, int=1); // prevent 0 at denominator! and reduce fraction and avoid -ive
friend RationalNumber operator+(const RationalNumber &r1,const RationalNumber &r2); // need & reference
friend RationalNumber operator-(const RationalNumber &r1,const RationalNumber &r2);
friend RationalNumber operator*(const RationalNumber &r1,const RationalNumber &r2);
friend RationalNumber operator/(const RationalNumber &r1,const RationalNumber &r2);
friend bool operator==(const RationalNumber &n1,const RationalNumber &n2);
friend bool operator!=(const RationalNumber &n1,const RationalNumber &n2);
friend bool operator>(const RationalNumber &n1,const RationalNumber &n2);
friend bool operator<=(const RationalNumber &n1,const RationalNumber &n2);
friend bool operator<(const RationalNumber &n1,const RationalNumber &n2);
friend bool operator>=(const RationalNumber &n1,const RationalNumber &n2);
void print();
private:
int denominator;
int numerator;
};
#endif /* defined(__Assignment_2Q1__RationalNumber__) */
.cpp 文件
#include <iostream>
#include "RationalNumber.h"
using namespace std;
RationalNumber(int n, int d)
{
if (d!=0&&d>0) {
if (d>=2||d<=-2) {
float fraction1;
float fraction2;
fraction1= n/d;
fraction2 = static_cast<float>(n)/d;
if (fraction1==fraction2) { // OR just simply 4/2 = 2 so n=2 , d=1!!!
numerator=fraction1;
denominator=1;
cout<<"You have entered a reductible fraction which reduces to! \n "<<numerator<<" / "<<denominator<<endl;
}
else{
numerator=n;
denominator=d;
cout<<"You have enterd an irreductible fraction! "<<numerator<<" / "<<denominator<<endl;
}
}
else
{
numerator=n;
denominator=d;
cout<<"The rational number is "<<numerator<<" / "<<denominator<<endl;
}
}
else
cout<<"The denominator cannot be 0 or a Negative number!"<<endl; // maybe add a set fct here
}
RationalNumber operator+(const RationalNumber& r1,const RationalNumber& r2)
{
return RationalNumber((r1.numerator+r2.denominator)/(r1.denominator+r2.denominator));
}
RationalNumber operator-(const RationalNumber &r1,const RationalNumber &r2)
{
return RationalNumber((r1.numerator-r2.denominator)/(r1.denominator-r2.denominator));
}
RationalNumber operator*(const RationalNumber &r1,const RationalNumber &r2)
{
return RationalNumber((r1.numerator*r2.denominator)/(r1.denominator*r2.denominator));
}
RationalNumber operator/(const RationalNumber &r1,const RationalNumber &r2)
{
return RationalNumber((r1.numerator/r2.denominator)/(r1.denominator/r2.denominator));
}
bool operator==(const RationalNumber &n1,const RationalNumber &n2)
{
return ((n1.numerator/n1.denominator)==(n2.numerator/n2.denominator));
}
bool operator!=(const RationalNumber &n1,const RationalNumber &n2)
{
return ((n1.numerator/n1.denominator)!=(n2.numerator/n2.denominator));
}
bool operator>(const RationalNumber &n1,const RationalNumber &n2)
{
return ((n1.numerator/n1.denominator)>(n2.numerator/n2.denominator));
}
bool operator<=(const RationalNumber &n1,const RationalNumber &n2)
{
return ((n1.numerator/n1.denominator)<=(n2.numerator/n2.denominator));
}
bool operator<(const RationalNumber &n1,const RationalNumber &n2)
{
return ((n1.numerator/n1.denominator)<(n2.numerator/n2.denominator));
}
bool operator>=(const RationalNumber &n1,const RationalNumber &n2)
{
return ((n1.numerator/n1.denominator)>=(n2.numerator/n2.denominator));
}
void RationalNumber::print()
{
cout<<numerator<<endl;
cout<<denominator<<endl;
}
主要的
#include <iostream>
#include "RationalNumber.h"
using namespace std;
int main()
{
RationalNumber object1(34, 73);
RationalNumber object2(5, 1);
RationalNumber object3(50, 25);
RationalNumber object4(87, 21);
RationalNumber object5(32, 0);
RationalNumber object6(32, -3);
RationalNumber object7(-32, 2);
RationalNumber object;
object = (object1 + object2);
object.print();
return 0;
}