这是我得到的错误 链接器错误未定义对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;
}
答案 0 :(得分:3)
更改
RationalNumber(int n, int d)
在你的.cpp
中RationalNumber::RationalNumber(int n, int d)
答案 1 :(得分:1)
您的.cpp文件函数被定义为自由函数。将RationalNumber ::放在它们的名称之前,将它们定义为类的成员函数。