3

This is the error that I get Linker error undefined reference to RationalNumber::RationalNumber(int, int)

I tried with Visual studio and the error says, cannot find the specified file rationalnumber.exe.

The Header file

#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__) */

The cpp file

#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;
}

The main

#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;
    }
will-hart
  • 3,742
  • 2
  • 38
  • 48
EEstud
  • 309
  • 1
  • 3
  • 12

2 Answers2

3

Change

RationalNumber(int n, int d)

in your .cpp to

RationalNumber::RationalNumber(int n, int d)
Steed
  • 1,292
  • 1
  • 14
  • 33
  • Agreed. This also needs to be done for the rest of the member functions for that class. – drescherjm Aug 06 '12 at 14:20
  • 1
    Almost all the other functions are friends there, so they don't need a prefix. And print() does have it already in author's code. – Steed Aug 06 '12 at 14:21
  • Oh, yeah, sorry, and thank you. I was messing with my program, but it is still not working I it should , the operator overloading is not adding objects properly. I wrote object = (object1 + object2); and the object is still assigned to its constructor value. – EEstud Aug 06 '12 at 14:23
  • So what is the other problem? – EEstud Aug 06 '12 at 14:27
  • @Steed - You are correct. I missed that one. – drescherjm Aug 06 '12 at 14:31
  • First, in your operator+ you call the RationalNumber constructor without specifying denomenator. It doesn't look like rational number addition: you just make an int division here (i.e. you add 1/5 + 1/3 and end up with 8/15, which gets converted to int 0). – Steed Aug 06 '12 at 14:32
1

Your .cpp file functions are defined as free functions. Put RationalNumber:: before their names to define them as member functions of the class.

Anon Mail
  • 4,660
  • 1
  • 18
  • 21