Frage

I have a function, that when defined in the main file of my program, works as I wish, and produces a lot of undefined references, when defined in a header file.

This header file exists:

#include "Domain.h"
#include "Character.h"

class Item {
    public:
        Character input;
        Item(Character c2);
};
Item pistol(int which, float strength);

The function that makes problems is pistol. It looks like

Item pistol(int which, float strength) {
    Interval i = Interval(0, 1);
    Domain d = Domain(i);
    Character c = Character({d}, {1});
    return Item(c);
}

When I try to link the code with my main program, all calls that refer to object in Domain.h and Character.h are undefined references, that means I get linking time errors like:

undefined reference to `Character::show()'
...
undefined reference to `Interval::Interval(float, float)'
...

these errors are at places in the code, which are not inside the pistol function.

When I move this function to my main program, everything works as expected:

#include "Domain.h"
#include "Character.h"
#include "Item.h"

Item pistol(int which, float strength) {
    // definition, see above
}

int main() {
    Item w2 = pistol(2, 0.5);
    return 0;
}

What is the problem with that function being in Item.h/Item.cxx? What do I need to do to put it their?

War es hilfreich?

Lösung

undefined reference is a linking stage error.

You most probably missed to link with a compilation unit, missed to recompile a dependent compilation unit, or tried to have a template class/function definition not seen by the compiler from all your compilation units using it!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top