What is the reason for the "Undefined symbols for x86_64 architecture:" error in C ++?

I would like to know why these files (or maybe others in my project) do not want to link. As far as I know, it's okay to leave the constructor and destructor if you define them in the .h file, right? I did it with another .h in my project and it doesn't complain. What's happening?

Error message:

Undefined symbols for architecture x86_64:
  "BasicArena::BasicArena()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


AreaInterface.h (for assignment purposes we are not allowed to edit this file)

class ArenaInterface
        virtual ~ArenaInterface(){}
        // lots of pure virtuals here



#ifndef __rpg__BasicArena__
#define __rpg__BasicArena__

#include <stdio.h>
#include "ArenaInterface.h"
#include "Cleric.h"

using namespace std;

class BasicArena : public ArenaInterface
    BasicArena() {}
    ~BasicArena() {}
    virtual bool addFighter(string info);
    virtual bool removeFighter(string name);
    virtual FighterInterface* getFighter(string name);
    virtual int getSize();

    vector<FighterInterface*> fighters;

#endif /* defined(__rpg__BasicArena__) */



#include "BasicArena.h"
#include <sstream>

using namespace std;
// member function definitions here



#ifndef __rpg__Cleric__
#define __rpg__Cleric__

#include <stdio.h>
#include <iostream>
#include "FighterInterface.h"

using namespace std;

class Cleric : public FighterInterface
    Cleric(string nam, int HP, int str, int spd, int mag, int dmg) : name(nam), maxHP(HP), strength(str), speed(spd), magic(mag), damage(dmg){}
    ~Cleric() {}
    // lots of functions and data



#include <iostream>
#include "BasicArena.h"

using namespace std;

int main() {
    BasicArena testarena;
    cout << "It works.\n";
    return 0;


UPDATE: Commenting out BasicArena declarations / definitions allows the program to work, but leaves me with the question of whether it is acceptable / desirable for the final program.


source to share

1 answer

Ok, I don't know exactly how it worked, but somehow after commenting out the constructor and destructor of BasicArena, running the program (it's compiled and running at this point), then restoring them, it compiles and works as far as I know the same code which he originally had. I'm at a loss. I'm just glad it works.

Most likely this code had a small bug (like a missing semicolon) that was causing the linker issue.



All Articles