I have the following two classes:
Foo which has constructor Foo(Bar*, int, int)
and Bar which calls new Foo(this, int, int)
I assumed that forward declaring Foo in Bar.h and/or Bar in Foo.h would solve the issue.
(The error returned is “undefined reference” on new Foo)
I”m using Clang to compile.
The linking order as it stands (but the same error happens in both cases) is Foo then Bar.
Any thoughts as to what I am doing wrong?
The code is roughly as follows. I cannot display any real code fragments unfortunately
#include Bar.h
class Bar ;
class Foo {
public:
Foo(Bar* bar, int arg1, int arg2) ;
void method1() {
I access bar->field here
}
And the code for Bar is then
#include Foo.h
class Bar {
public:
Bar() {
Cache* cache = new Cache(this, 0, 0) ;
}
It should look something like this (leaving out include guards):
foo.h
bar.h
foo.cc
bar.cc
If you’re getting “undefined reference” from the linker, you probably declared
Foo::Foowith a different signature than you defined it with, or did not define it at all, or are not linking with the object file it’s compiled into.