0

I get this error:

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

when trying to compile the following code:

human.h

class Human {
    private:
        int age;
        int height;
    public:
        Human();
        Human(int age, int height);
        int getAge();
};

human.cpp

#include "human.h"

Human::Human(){
    this->age = 30;
    this->height = 20;
}

Human::Human(int age, int height){
    this->age = age;
    this->height = height;
}

int Human::getAge(){
    return this->age;
}

main.cpp

#include <iostream>

// doesn't work
// #include "human.h"

// works
#include "human.cpp"

using namespace std;

int main(){
    Human bean = Human(69, 10);
    cout << "bean's age is: " << bean.getAge() << endl;
    return 0;
}

However, my code compiles when I include only human.cpp, but does not when I include only human.h.

I have tried to find an answer to this question but have had no success. I would appreciate any insight as to why this occurs.

alpharoz
  • 119
  • 9

0 Answers0