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?

4

2 に答える 2

7

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

于 2012-06-20T17:10:34.347 に答える
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.

于 2012-06-20T17:14:04.577 に答える