1

Hey guys I asked a question the other day about some c++ code that I couldn't get to work. I took everyones advice as to how to create objects in c++ but now I get undefined reference errors. I am using the latest code blocks version and using that to compile. I have read that this is caused by not linking some files during compilation, and that it means I have defined the class in the header file but not in the code, which confuses me because from my understanding (a profs example) I am declaring the objects.

Header File MathObject.h

class MathObject{
private:
    int num1;
    int num2;

public:
    int sum();
    MathObject(int n, int m);
};

MathObject file MathObject.cpp

#include <iostream>
#include "MathObject.h"
using namespace std;

MathObject :: MathObject(int n, int m){
    num1 = n;
    num2 = m;
}

int MathObject :: sum(){
    return num1+num2;
}

Main File

#include <iostream>
#include "MathObject.h"
using namespace std;

int main(int args, char *argv[]){
    MathObject *mo = new MathObject(3,4);
    int sum = mo -> sum();

    MathObject mo2(3,4);

    //cout << sum << endl;
    return 0;
}

The undefined reference is for all calls to anything in the MathObject class, I have been searching for a small c++ example that I can understand. (The syntax is so different from java)

This used to happen when I tried to use multiple files in c, could this be an issue with my computer?

M.M
  • 138,810
  • 21
  • 208
  • 365
Michael Miner
  • 964
  • 2
  • 17
  • 39

6 Answers6

7

In the "Projects" tab in codeblocks, right-click your project's name and select "Add Files..."

Alternately, you can choose "Add files..." from "Project" in the application's main menu.

Use this to add all of your source files to your project.

Currently MathObject.cpp is missing from that list, so it's not getting compiled or linked.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • oddly, clicking the "Add Files" under "Project" does nothing. The system or ide does not hang, it is simply as if there is no action set for that selection. – Michael Miner Jun 17 '14 at 00:31
  • Make sure you've selected all required files under _Project > Properties > Build targets > **Build target files**_ – Ganapathy Feb 23 '18 at 16:22
  • If this does not work then right click each files and click ```add to active project``` . Then it would be added to the project manually. – Seungju Mar 11 '20 at 19:10
0
g++ MathObject.cpp main.cpp -o main
Ruslan Gerasimov
  • 1,752
  • 1
  • 13
  • 20
  • tried, this. I did a cd to the folder all the files are in and typed your line exactly as you have and I am told that g++ is not a recognized command – Michael Miner Jun 17 '14 at 00:34
  • @MichaelMiner Add the path to the `MinGW/bin` folder to the `PATH` environment variable. – David G Jun 17 '14 at 00:36
  • What is your platfrom\operating system? It is just the example that you need to include MathObject.cpp file to compiling. I ran it from Linux, which has g++ compiler by default. – Ruslan Gerasimov Jun 17 '14 at 00:36
0

Found a solution from code::blocks forum:

-Project -> "Build Options

-Make sure the correct target is highlighted on the left side; if you do not know select the project, top one.

-Select Tab "Search Directories"

-Select Sub-Tab "Compiler"

-"Add" the path to the folder that contains the header. Single Folder per line.

Just add your current folder or location of your header file to the path.

Link: http://forums.codeblocks.org/index.php?topic=14713.0

impulse
  • 206
  • 2
  • 15
0

You can do this simply by adding .cpp file of the class in main.cpp.

#include <iostream>
#include "MathObject.h"
#include "MathObject.cpp"
using namespace std;

int main(int args, char *argv[]){
    MathObject *mo = new MathObject(3,4);
    int sum = mo -> sum();

    MathObject mo2(3,4);

    //cout << sum << endl;
    return 0;
}
0

To fix undefined reference error :-

  • Settings -> compiler... -> Build options finally mark "Explicitly add currently compiling file's directory to compiler search dirs"
  • Welcome to Stack Overflow! Please note you are answering a very old and already answered question. Here is a guide on [How to Answer](http://stackoverflow.com/help/how-to-answer). – help-info.de Aug 17 '20 at 12:17
-3

I try this and works fine!

MAIN.cpp

#include <iostream>
#include "MathObject.h"
using namespace std;

int main(int args, char *argv[]){
    MathObject *mo = new MathObject(3,4);
    int sum = mo->sum();

    MathObject mo2(3,4);
    int sum2 = mo2.sum();

    cout << sum << endl;
    cout << sum2 << endl;
    system("pause");
    return 0;
}

MathObject.h

class MathObject
{
private:
    int num1;
    int num2;
public:
    MathObject(void);
    ~MathObject(void);
    int sum();
    MathObject(int n, int m);
};

MathObject.cpp

#include "MathObject.h"

MathObject::MathObject(void)
{
}

MathObject::~MathObject(void)
{
}
int MathObject::sum(){
    return num1+num2;
}
MathObject::MathObject(int n, int m){
    num1 = n;
    num2 = m;
}

Compile with:

g++ MathObject.cpp main.cpp -o main.exe
Esteban E
  • 65
  • 1
  • 5
  • using yours, I still get unreferenced errors, could it be my compiler or ide? – Michael Miner Jun 17 '14 at 00:29
  • 1
    This does not answer the question. The OP is obviously experiencing something different from you on his compiler and simply asserting that it "works for you" doesn't help the situation. – David G Jun 17 '14 at 00:29
  • i forgot comment how i compile it: g++ MathObject.cpp main.cpp -o main.exe – Esteban E Jun 17 '14 at 00:30
  • if yours works, does that not mean mine should as well? I have tried compiling it that way but still nothing – Michael Miner Jun 17 '14 at 00:38
  • 1
    then you could give more details of your problem, the exact error that gives the compiler or any clue... – Esteban E Jun 18 '14 at 01:13