0

My have three .cpp files and their headers.

    //a.cpp
#include "a.h"
#include "b.h"
void A::foo() {
    C c;
    c.bar();
}


    //a.h
#include "b.h"
class A {
public:
    void foo();
};


    //b.h
#include "c.h"

    //c.h
#pragma once    
class C {
public:
    void bar();
};


    //c.cpp
#include "c.h"
void C::bar() {}

    //other files are ignored

But when I compiled them, I got this error:

a.cpp:(.text+0xb1): undefined reference to `C::bar()`

Have I included c.h through b.h? Why doesn't it work?

Lai Yu-Hsuan
  • 27,509
  • 28
  • 97
  • 164
  • Sorry looks like I forget to clean some old object files. I should run `make clean` first... It's a unqualified question indeed. – Lai Yu-Hsuan Jun 20 '12 at 17:15

2 Answers2

7

An undefined reference is a linker error, your code is compiling. Make sure you are linking a.obj, b.obj and c.obj.

IronMensan
  • 6,761
  • 1
  • 26
  • 35
2

This is a link error, indicating that the definition of C::bar() is missing from the set of translation units that are linked to build the program. The most likely reason is that you're not including c.cpp in your build.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644