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?