-2

guys i am having a weird problem it has been working fine but suddenly messed up Here is the problem. I have test.h and test.cc and mainP.cc.

In mainP.cc:

#ifndef test_H
#include "test.h"
#endif

in test.h:

#ifndef test_H
#define test_H

in test.cc:

#ifndef test_H
#include "test.h"
#endif

now when I say test tInstance; it gives me undefined reference to test::test(); I checked my def. constr. is public . everything looks ok to me. I am not sure what i wrong? i use g++ to compile it.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 2
    Isn't test.h missing an `#endif`? In any case, drop the `#ifdef` from the .cc files, the include guards should only be in the header. – Ulrich Eckhardt May 01 '14 at 06:33
  • Don't use include guards in the files that `#include "test.h"` - only have the include guards in `test.h` itself. Though I doubt this is related to your problem. I don't think you've included enough information for anyone to help with your problem. – Michael Burr May 01 '14 at 06:36

1 Answers1

0

From the sparse information so far it sounds like you're not linking the files together properly, and you're trying to build main.cc without having test.cc linked to it, therefore it cannot find the definition of the function. Check your command line.

The header guards are wrong for the reasons noted in the comments (don't put guards around the #include, only in the .h file itself, but I don't think they are the cause of the problem.

This seems to be a duplicate of G++ undefined reference to class::function

Community
  • 1
  • 1
Zebra North
  • 11,412
  • 7
  • 37
  • 49