1

Possible Duplicate:
“undefined reference to” in G++ Cpp

I have this header

#ifndef TEST_H_
#define TEST_H_

class TEST
{
public :
    TEST();
};


#endif /* TEST_H_ */

and this header

#ifndef TESTS_H_
#define TESTS_H_
#include "TEST.h"

class TESTS : public TEST
{
public :
    TESTS();
};


#endif /* TESTS_H_ */

I implemented those header like this:

#include <iostream>
using namespace std;
#include "TEST.h"
TEST:: TEST()
{
}
int main(int argc, char **argv) {
    return 0;
}

and this:

#include "TESTS.h"

TESTS :: TESTS() : TEST()
{
}


int main(int argc, char **argv) {
    return 0;
}

and I get the follwing error:

/tmp/cc4jN1HN.o: In function TESTS::TESTS()': TESTS.cpp:(.text+0xd): undefined reference toTEST::TEST()'

Why is it ?

What am I doing wrong ?

Community
  • 1
  • 1
John Oldman
  • 89
  • 2
  • 8
  • 3
    You can't ask us what you're doing wrong if you don't tell us what you're *trying* to do! And it's far from clear: Why do you have two `main` functions?! A program can only have one. Is this supposed to be two programs or one program? – David Schwartz Dec 31 '12 at 08:40

3 Answers3

6

This is a linker error. You're probably not compiling and linking all the required files. It should be e.g.:

$ g++ -Wall TEST.cpp TESTS.cpp -o TEST

You also need to get rid of one of the main() functions.

Paul R
  • 208,748
  • 37
  • 389
  • 560
1

If this is supposed to be one program, it can only have one main function. If this is supposed to be two programs, then the one with TESTS::TESTS in it also needs the constructor for TEST.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

Assuming your two .cpp files are named TEST.cpp and TESTS.cpp, I'm guessing you're just compiling the first one, such as with

g++ TESTS.cpp

but you need to compile them both together, such as with

g++ TEST.cpp TESTS.cpp

otherwise, TESTS.cpp won't be able to find the code defined in TEST.cpp when it's compiling.

Verdagon
  • 2,456
  • 3
  • 22
  • 36