-1

I'm getting undefined reference errors. Can you please figure out why?

Errors:

  • undefined reference to Breuken::Breuken(int, int)
  • undefined reference to Breuken::som()

(I'm kinda new in c++ programming. I am currently working on a project now, using header files another source file and main source file.)

Here is my code:

Header File (Breuk.H):

using namespace std;
class Breuken{
    Breuken(int, int);
    void som();

    void setTeller1(int);
    void setNoemer1(int);
    int getTeller1();
    int getNoemer1();

private:

    int teller1;
    int noemer1;
};

Source File(Breuk.cpp):

#include <iostream>
#include "Breuk.h"

using namespace std;

Breuken::Breuken(int tel1, int noem1)
{
    setTeller1(tel1);
    setNoemer1(noem1);
}

int Breuken::getTeller1()
{
    return teller1;
}
int Breuken::getNoemer1()
{
    return noemer1;
}

void Breuken::setTeller1(int tel1)
{
    teller1 = tel1;
}
void Breuken::setNoemer1(int noem1)
{
    noemer1 = noem1;
}
int Breuken::som()
{
    cout<<"Breuken zijn: "<<getTeller1()<<" / "<<getNoemer1()<<endl;
}

main file(main.cpp):

#include <iostream>
#include "Breuk.h"

using namespace std;

int main()
{
    Breuken br(1, 2);
    br.som();
    return 1;
}
JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 3
    You're probably not linking your object files correctly. – Aplet123 Apr 30 '20 at 22:49
  • Error messages often contain useful information, so please copy and paste the exact messages into the post instead of trying to paraphrase them. – eesiraed Apr 30 '20 at 22:54
  • Try adding `public` in the class def? How are you compiling? – Rotartsi Apr 30 '20 at 22:57
  • 1
    how are you building this code? – pm100 Apr 30 '20 at 22:57
  • I'm using codeblocks. I know the theory of it. But I can't see the problem in my code. – Parwesh Bhaggan Apr 30 '20 at 23:01
  • I've tried using the exact codes in only main file and it works!. But I need to work with these header files and stuff. – Parwesh Bhaggan Apr 30 '20 at 23:05
  • @ParweshBhaggan Since it works with only one file, there is probably something wrong with how you build your project. As Aplet123 already pointed out, you are probably [not linking your object files correctly](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574400#12574400). Possibly you are not even generating all of your object files. *(A quick check for this would be to intentionally add a syntax error to `Breuk.cpp` and make sure the compiler complains about it.)* – JaMiT May 01 '20 at 00:38

1 Answers1

-1

If I got this right, you want to use a class, but the only thing you did is declare some functions without actually defining them.

What you need to do is put everything in your header inside a class like that:


class Breuk {
private:
    int teller1;
    int noemer1;

public:
    Breuk(int tel1, int noem1);

    void som();

    void setTeller1(int tel1);
    void setNoemer1(int noem1);

    int getTeller1();
    int getNoemer1();
};

Also, don't forget to give the variables inside the function head a name!

The .cpp file you need to get the reference to the class right (the part before the ::). It has to be the same name as your class. Also, it's not so great to use setter inside the constructor. It's better to reference the member variables directly (the variables inside the class). In order to do that you need to use "this->variable". "this" will refer to the current class you're in. That means if you write this->teller1 it will refer to the variable teller1 inside the class.

#include <iostream>
#include "Breuk.hpp"

using namespace std;

Breuk::Breuk(int tel1, int noem1)
{
    // setTeller1(tel1);   theoreticly ok but its better to access
    // setNoemer1(noem1);  member variables (variables inside a class) directly

    this->teller1 = tel1;
    this->noemer1 = noem1;
}

int Breuk::getTeller1()
{
    return teller1;
}
int Breuk::getNoemer1()
{
    return noemer1;
}

void Breuk::setTeller1(int tel1)
{
    teller1 = tel1;
}
void Breuk::setNoemer1(int noem1)
{
    noemer1 = noem1;
}
void Breuk::som()
{
    cout << "Breuken zijn: " << getTeller1() << " / " << getNoemer1() << endl;
}

in the main.cpp you only made a small mistake with the return 0 and the name of the class.


#include "Breuk.hpp"

using namespace std;

int main()
{
    Breuk br(1, 2); // make sure u use the same name your class has

    br.som();

    return 0; // return 0 at the end: it means that the program executed correctly
              // return 1 would mean that an error occured
}

Also, I suggest using .hpp files instead of .h files. There is no real difference but it's more common to use .hpp for c++.

  • still the same... – Parwesh Bhaggan Apr 30 '20 at 23:21
  • Have you tried using a different compiler? Because I could compile it without problems. If you use a Linux machine I would suggest you g++ or on windows MinGW. They are not very beginner-friendly but you'll find many tutorials by googling it. – mandMister Apr 30 '20 at 23:30