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);

declaration in header file

class fraction{
  fraction addFractions(fraction two);
5 Years
Discussion Span
Last Post by James19142

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.


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);

    return a.exec();

this is the whole header

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

    fraction(int whole,int top,int bottom)
    double percent();

    fraction addFractions(fraction two);

    fraction subtractFractions(fraction two);

    fraction multiplyFractions(fraction two);

    fraction divideFrations(fraction two);

    fraction simplified();

    fraction noIPF();

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

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.