Michael Miner
Michael Miner

Reputation: 964

Codeblocks c++ undefined reference error, class is defined

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?

Upvotes: 1

Views: 28064

Answers (6)

Nardos Wehabe
Nardos Wehabe

Reputation: 1

To fix undefined reference error :-

  • Settings -> compiler... -> Build options finally mark "Explicitly add currently compiling file's directory to compiler search dirs"

Upvotes: 0

Hunterrr
Hunterrr

Reputation: 1

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

Upvotes: 0

impulse
impulse

Reputation: 206

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

Upvotes: 0

Esteban E
Esteban E

Reputation: 65

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

Upvotes: -3

Drew Dormann
Drew Dormann

Reputation: 63820

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.

Upvotes: 9

Ruslan Gerasimov
Ruslan Gerasimov

Reputation: 1782

g++ MathObject.cpp main.cpp -o main

Upvotes: 0

Related Questions