-1

I'm pretty new to C++ and I keep getting undefined reference errors to constructors although they are defined and included. I know this is a pretty common error and there's many questions about this, but each one seems a bit different from mine and I am unable to resolve my problems.

Basically I want a class B to have an instance variable of class A, which is a virtual class with a "real" subclass ASub. Also, the files for class A are in ~/level1/ and the files for class B are in ~/level1/level2/.

I keep getting the following errors:

error: undefined reference to 'mynamespace::A::A()'
error: undefined reference to 'mynamespace::A::~A()'

a.h looks like this:

#ifndef A_H__
#define A_H__

namespace mynamespace {

class A {
 public:
  A();
  virtual ~A();
};

class ASub : public A {
 public:
  ASub();
  ~ASub();
};

} // namespace mynamespace

#endif  // A_H__

a.cc looks like this:

#include "level1/a.h"

namespace mynamespace {

A::A() {}
A::~A() {}

ASub::ASub() {}
ASub::~ASub() {}

};  // namespace mynamespace

b.h looks like this:

#ifndef B_H__
#define B_H__

#include "level1/a.h"

namespace mynamespace {

class B {
 public:
  B();
  ~B();
  void SetA(A a);

 private:
  A a_;
};

} // namespace mynamespace

#endif  // B_H__

And finally b.cc looks like this:

#include "level1/level2/b.h"
#include "level1/a.h"

namespace mynamespace {

B::B() {}
B::~B() {}

void B::SetA(A a) {
  a_ = a;
}

};  // namespace mynamespace
niefpaarschoenen
  • 560
  • 1
  • 8
  • 19
  • 2
    It shouldn't compile: while processing b.h, `A` is not declared. The code you show is not the code you are actually building. – Igor Tandetnik Sep 03 '14 at 22:53
  • How are you linking your object files? (I'm also doubtful that `b.h` can compile since `A`'s declaration is missing.) Also, `A_H__` is a reserved identifier because it has a double underscore. Don't use it. – T.C. Sep 03 '14 at 22:55
  • Show us the command line that you're using to build the project. – randomusername Sep 03 '14 at 22:55
  • @IgorTandetnik: you are right, I forgot an include in b.h. Edited it now... – niefpaarschoenen Sep 03 '14 at 22:59
  • 1
    If you are still getting those linker errors, then I would guess you are not actually building, or linking with, `a.cc`. Double-check your build configuration. As a quick text, type random garbage into `a.cc` and build: if the compiler doesn't complain, you know it's not looking at that file. – Igor Tandetnik Sep 03 '14 at 23:19
  • make sure when you complile , you compile a.cc and b.cc, `undefined reference to` means linker doesn't find the implement of the function, I think you doesn't complie a.cc – frank.lin Sep 04 '14 at 02:32
  • The problem was indeed not with the code but with the build, sorry about that. – niefpaarschoenen Sep 04 '14 at 03:22

1 Answers1

1

Include a.h in b.h, otherwise you won't be able to compile. The linker error is likely from a previous build, I suspect you need to do a clean build.

ventsyv
  • 3,316
  • 3
  • 27
  • 49