954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

compiler cant find .cpp file for my header file

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);
};
James19142
Newbie Poster
18 posts since Oct 2009
Reputation Points: 6
Solved Threads: 0
 

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.

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

It should be

fraction fraction::addFractions(fraction two)
{
//...
}
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

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();
}
James19142
Newbie Poster
18 posts since Oct 2009
Reputation Points: 6
Solved Threads: 0
 

Where's your constructor that accepts three integers?

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

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);}
  };
James19142
Newbie Poster
18 posts since Oct 2009
Reputation Points: 6
Solved Threads: 0
 

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.

James19142
Newbie Poster
18 posts since Oct 2009
Reputation Points: 6
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: