2

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.

NawaMan
  • 25,129
  • 10
  • 51
  • 77
  • 1
    Is the space in "# include" in your main file or just a typo here? – John Meschke Jun 20 '20 at 06:25
  • Are you sure you have the right compiler architecture set? `Undefined symbols for architecture x86_64` seems to say that you have the wrong compiler – kesarling He-Him Jun 20 '20 at 06:28
  • Add `-Wshadow` to your compile string. `arbitaryNumber`, `lastName` and `firstName` are all shadowed. – David C. Rankin Jun 20 '20 at 06:29
  • This code builds successfully in my machine :) Is this native to Eclipse? – kesarling He-Him Jun 20 '20 at 06:36
  • Problem is that you are building sources separately and do not link them. Do: `g++ main.cpp person.cpp` or fix project file your IDE is using. – Marek R Jun 20 '20 at 07:06
  • @JohnMeschke -- it's okay to put whitespace between `#` and the rest of a preprocessor directive. In `#include`, the `#` and the `include` are two separate tokens; whitespace is optional, just like `a+b` and `a + b`. – Pete Becker Jun 20 '20 at 12:28
  • @John Meschke My code actual have no space ... just when I copied it somehow I added in by accident. – NawaMan Jun 20 '20 at 17:55
  • @d4rk4ng31 When I comment out `Person p(...);`, it built and ran just fine so I think this should be ok. – NawaMan Jun 20 '20 at 17:56
  • @David C. Rankin : I tried `g++ -Wshadow ExampleMake.cpp -o ExampleMake.o` but still get the same error. Am I doing it right? – NawaMan Jun 20 '20 at 17:57
  • `Person::Person(std::string first, std::string last, int number):firstName(first), lastName(last), arbitaryNumber(number) {}` – David C. Rankin Jun 21 '20 at 00:17
  • Thanks @David C. Rankin , I have the constructor implementation. – NawaMan Jun 21 '20 at 03:17
  • If you are still stuck `g++ -Wall -Wextra -pedantic -Wshadow -std=gnu++11 -Ofast person.cpp -o main main.cpp` – David C. Rankin Jun 21 '20 at 05:23
  • Thanks @David C. Rankin , That's work!!!. If you write it as an answer (may be with explanation), I will select. :-) – NawaMan Jun 21 '20 at 14:50
  • 1
    No worries, just glad it helped. Good luck with your coding! – David C. Rankin Jun 21 '20 at 22:19
  • So, the answer that @DavidC.Rankin gave above is correct but if you want to make it work with CMake (which I am new at), you need to add the files into CMakeLists.txt file: `add_executable(CMakeExample CMakeExample.cpp Person.cpp Person.h)` – NawaMan Jun 24 '20 at 01:05

1 Answers1

0
g++ -o ExampleMake ExampleMake.o

You are linking just one file, when you have 2 cpp files.

maniek
  • 7,087
  • 2
  • 20
  • 43