0
//main.cpp
#include <iostream>
#include "Test.h"

using namespace std;

int main() {
    Test t;
    return 0;
}


//Test.h
#ifndef TEST_TEST_H
#define TEST_TEST_H


class Test {
public:
    Test();
};


#endif //TEST_TEST_H


//Test.cpp
#include "Test.h"
#include <iostream>

using namespace std;

Test::Test() {
    cout << "It worked";
}

I just started using C++ and I cannot figure out why this is not compiling. I am using the mingw compiler and CLion if that makes a difference with the error, I don't see why it would but I am being told that I need more detail...

Error message

C:\Users\Jason\AppData\Local\Temp\ccQ7aCrg.o:main.cpp:(.text+0x15): undefined reference to `Test::Test()'
collect2.exe: error: ld returned 1 exit status
  • Is `Test.cpp` being compiled? The error implies that it can't find `Test::Test` when linking. – lcs Jul 28 '15 at 17:38
  • If the link in the 1st comment doesn't resolve your issue please include the compile(link) command that triggers the error. – CristiFati Jul 28 '15 at 17:41
  • 1
    It is compiling all right, it's not linking. It appears that you're only linking code from `main.cpp` and not `Test.cpp`. See [this answer](http://stackoverflow.com/a/15622496/1329652). To compile and link multiple files, do: `g++ -o program.exe main.cpp Test.cpp`. This will compile both `.cpp` files and link the results into `program.exe`. – Kuba hasn't forgotten Monica Jul 28 '15 at 17:41
  • This question is not the duplicate everyone thinks of. It is a duplicate of [this question](http://stackoverflow.com/q/15622409/1329652) instead. – Kuba hasn't forgotten Monica Jul 28 '15 at 17:42
  • @NathanOliver Either we have the same misconceptions how that dupe should be helpful for newbs, or are finally right off many times along. Keep on flagging my friend. – πάντα ῥεῖ Jul 28 '15 at 17:43
  • @KubaOber The marked dupe covers that, believe it or not, it's really an awesome Q&A covering almost all of the situations that could cause that kind of linker error. – πάντα ῥεῖ Jul 28 '15 at 17:46
  • @πάνταῥεῖ Yeah, but the asker will not know which part of the wall of text in the supposed dupe is the answer that will help them. This looks like a rather straightforward newb mistake that my proposed dupe covers exactly. – Kuba hasn't forgotten Monica Jul 28 '15 at 17:50
  • @KubaOber At least mismatch of tags would deny to select your proposal as duplicate. All of the answers show compiling c code, rather than c++ code. Anyway I don't bother with the additional information given from your link, hopefully the OP is intelligent enough to extrapolate. – πάντα ῥεῖ Jul 28 '15 at 17:52
  • @πάνταῥεῖ Thankfully gcc is cleverer than that :) – Kuba hasn't forgotten Monica Jul 28 '15 at 17:53
  • @KubaOber your explanation worked so thank you – internetRando Jul 28 '15 at 17:57

0 Answers0