0

I'm just learning constructor and destructor I'm following the tutorial this guy is doing bucky tutorial. I think the video is outdated? since I followed every single step he says and i'm still getting an error.

main.cpp

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

using namespace std;

int main(){

    TESTING so;
    cout << "TEST" << endl;

}

TESTING.h

#ifndef TESTING_H
#define TESTING_H


class TESTING
{
    public:
        TESTING();
    protected:
        private:
};

#endif // TESTING_H

TESTING.cpp

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


TESTING::TESTING()
{
    cout << "TESTTTTT!!" << endl;
}

Error messages

\main.o:main.cpp undefined reference to 'TESTING::TESTING()'

Build log

mingw32-g++.exe   -c D:\C++\TESTING!\main.cpp -o D:\C++\TESTING!\main.o
mingw32-g++.exe  -o D:\C++\TESTING!\main.exe D:\C++\TESTING!\main.o   
D:\C++\TESTING!\main.o:main.cpp:(.text+0x52): undefined reference to `TESTING::TESTING()'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
1 error(s), 0 warning(s) (0 minute(s), 1 second(s))
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621

2 Answers2

1

You only build and link the main source file, not the TESTING source file. You need to compile TESTING.cpp as well, and then link with TESTING.o:

mingw32-g++.exe   -c D:\C++\TESTING!\TESTING.cpp -o D:\C++\TESTING!\TESTING.o
mingw32-g++.exe  -o D:\C++\TESTING!\main.exe D:\C++\TESTING!\main.o D:\C++\TESTING!\TESTING.o
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

You need to include both the compilation units in the build.

That means both source files need to be compiled, and both corresponding object files specified in the link command.

At present, only main.cpp is being compiled to an object, and only main.o is being linked.

Peter
  • 35,646
  • 4
  • 32
  • 74