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?

6
  • 2
    add MathObject.cpp to the files you are compiling Commented Jun 17, 2014 at 0:17
  • How are you building this project? From the command line? Commented Jun 17, 2014 at 0:20
  • @DrewDormann with the codeblocks ide built in compiler Commented Jun 17, 2014 at 0:21
  • @RedAlert, Do you mean with an include statement? I thought you only need to include the header file. Commented Jun 17, 2014 at 0:21
  • an include statement would probably resolve this issue, but that's not how you want to fix it. You need to compile both source files as part of your project, not just main.cpp Commented Jun 17, 2014 at 2:06

6 Answers 6

9

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
Make sure you've selected all required files under Project > Properties > Build targets > Build target files
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.
0
g++ MathObject.cpp main.cpp -o main

3 Comments

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
@MichaelMiner Add the path to the MinGW/bin folder to the PATH environment variable.
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.
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

Comments

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

Comments

0

To fix undefined reference error :-

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

1 Comment

Welcome to Stack Overflow! Please note you are answering a very old and already answered question. Here is a guide on How to Answer.
-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

5 Comments

using yours, I still get unreferenced errors, could it be my compiler or ide?
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.
i forgot comment how i compile it: g++ MathObject.cpp main.cpp -o main.exe
if yours works, does that not mean mine should as well? I have tried compiling it that way but still nothing
then you could give more details of your problem, the exact error that gives the compiler or any clue...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.