0

How can I get this code to work?

main.cpp

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

using namespace std;

int main()
{
TestClass testclass;
int number = testclass.AddNumbers(3, 5);

cout << number << endl;
return 0;
}

TestClass.h

#ifndef TESTCLASS_H
#define TESTCLASS_H 


class TestClass
{
    public:
        TestClass();
        int AddNumbers(int num1, int num2);
    protected:
    private:
};

#endif // TESTCLASS_H

TestClass.cpp

#include "TestClass.h"

TestClass::TestClass(){

}

int TestClass::AddNumbers(int num1, int num2){
    return num1 + num2;
}

The only error I get is "Undefined reference to TestClass::TestClass() and "Undefined reference to TestClass::AddNumbers(int, int)".

1 Answers1

2

When you're compiling two .cpp files, you will get as a result two "object files". These are intermediate files containing the compiled content. After that, you need to link them to the actual executable binary.

Have a look at the link Captain Oblivous provided and check out the second answer. It explains this in greater deep.

Zuppa
  • 467
  • 5
  • 16