I am learning C++ and stuck with what seems to be a super simple thing. :-(
Here is my Person.h
#include <string>
class Person {
private:
std::string firstName;
std::string lastName;
int arbitaryNumber;
public:
Person(std::string firstName, std::string lastName, int arbitaryNumber);
std::string getName();
};
My CPP file is:
#include "Person.h"
Person::Person(std::string firstName, std::string lastName, int arbitaryNumber):
firstName(firstName), lastName(lastName), arbitaryNumber(arbitaryNumber) {
}
std::string Person::getName() {
return this->firstName + " " + this->lastName;
}
So far things are super simple. Let's used it in the main.
#include <iostream>
# include "Person.h"
int main(int argc, char **argv) {
Person p("Nawa", "Man", 100);
return 0;
}
When I compile/run my code, I got this error.
Building in: /Users/nawa/eclipse-workspace-CPP/ExampleMake/build/default
make -f ../../Makefile
g++ -c -O2 -o ExampleMake.o /Users/nawa/eclipse-workspace-CPP/ExampleMake/ExampleMake.cpp
g++ -o ExampleMake ExampleMake.o
Undefined symbols for architecture x86_64:
"Person::Person(std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >, int)", referenced from:
_main in ExampleMake.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [ExampleMake] Error 1
Build complete (2 errors, 0 warnings): /Users/nawa/eclipse-workspace-
CPP/ExampleMake/build/default
What is wrong with the constructor? I am using Eclipse on Mac.
I suspected that it has something to do with string literal and str::string but I am not sure. So, when I compiled it in the command line, I got the same error so this does not seems to be Eclipse problem.
Please help. Thanks.