i made a header file with my function declarations and the cpp file with the definitions. While making the definitions in the .cpp file i included the header and from the highlighted text the definitions seemed to match declarations in the header. In the header the the declarations were able to show where each definition is located. But when i run a test, i get error that appears to be saying the definition cant be found.

C:\Users\JAMES\programs\ClassTest-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\..\ClassTest\main.cpp:13: error: undefined reference to `fraction::addFractions(fraction)'

cpp file

#include "Jims header.h"
fraction::addFractions(fraction two)
{
    int a1=_whole*_bottom+_top;
   int a2=two.Whle()*two.Den()+two.Num();
    int b1=one.Den();
    int b2=two.Den();
    int m_n1=a1*b2;
    int m_n2=a2*b1;
    int d=b1*b2;
    int a_answer_n=m_n1+m_n2;
    int a_answer_d=d;
    fraction added(a_answer_n,a_answer_d);
    return(added);
}

declaration in header file

class fraction{
public:
  fraction addFractions(fraction two);
};

Recommended Answers

All 6 Replies

line 2 in your in the cpp file should be

fraction fraction::addFractions(fraction two)

The reason it was not working they way you had it was because you did not have the return type in the deceleration.

It should be

fraction fraction::addFractions(fraction two)
{
//...
}

i was thinking that was the problem before posting the last thread so i tried it already and then changed it back after receiving the same error, after reading your reply i put the return type back in the declaration. I just started using the "class" part of c++ so i might have a few mistakes.

i forgot paste the code in the program

#include <QtCore/QCoreApplication>
#include "Jims header.h"
#include "iostream"

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    fraction myfraction(0,50,8);
    fraction myotherfraction(4,3,2);
   fraction newfraction=myfraction.addFractions(myotherfraction);
    cout<<newfraction.Den()<<endl;


    return a.exec();
}

Where's your constructor that accepts three integers?

this is the whole header

class fraction{
    int _top;
    int _bottom;
    int _whole;

public:
    fraction(int whole,int top,int bottom)
    {
      _top=top;
      _bottom=bottom;
      _whole=whole;
   }
    double percent();

    fraction addFractions(fraction two);

    fraction subtractFractions(fraction two);

    fraction multiplyFractions(fraction two);

    fraction divideFrations(fraction two);

    fraction simplified();

    fraction noIPF();

     int Den()
    {return(_bottom);}
    int Num()
    {return( _top);}
    int Whle()
   {return(_whole);}
  };

Thanks for all the help but i found the problem, i didnt list the the .cpp file that contains the definitions for my header in the .pro file. My program runs find now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.