0

I have exhausted myself trying to figure this issue out, so maybe more knowledgeable people can help me. I am taking an online coding course and have encountered an issue I can't seem to solve. I have paused the video multiple times and copied everything the instructor has done, but his code compiles and mine keeps getting an error message. The current subject of the course is inheritance between classes, and I have two .cpp files and two header files. I will post each page of code and then the error message I keep getting. What am I doing wrong? And why did it work for him and not me?

Main.cpp

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

int main(){
    Player p1("Basketball");
    std::cout <<"player : " << p1 <<std::endl;

    return 0;
}

person.h

#include <iostream>
#include <string>
#include <string_view>

class Person {
    friend std::ostream& operator << (std::ostream& out, const Person& person);
    public:
    Person();
    Person(std::string& first_name_param, std::string& last_name_param);
    ~Person();

    std::string get_first_name() const{
        return first_name;
    }

    std::string get_last_name() const{
        return last_name;
    }

    private:
    std::string first_name{"Mysterious"};
    std::string last_name{"Person"};
};

player.h

#include <iostream>
#include <string>
#include <string_view>
#include "person.h"

class Player : public Person{
    friend std::ostream& operator << (std::ostream& out, const Player& player);
public:
    Player() = default;
    Player(std::string_view game_param);
    
private:
    std::string m_game{"None"};

};

player.cpp

#include "person.h"
#include "player.h"


Player::Player(std::string_view game_param)
: m_game(game_param){

}

std::ostream& operator<<(std::ostream& out, const Player& player){
    out << "Player: [ game: " << player.m_game << " names: " << Player.get_first_name() << " " << Player.get_last_name() << "]";
    return out;
}

person.cpp

#include "person.h"
 
 Person::Person(){}

Person::Person(std::string& first_name_param, std::string& last_name_param)
    : first_name(first_name_param), last_name(last_name_param) {
    }

std::ostream& operator << (std::ostream& out, const Person& person){
    out << "Person [" << person.first_name << " " << person.last_name << "]";
    return out;
}

Person::~Person(){

}

These are my different files and here is the error I keep getting:

Executing task: C/C++: g++.exe build active file 

Starting build...
C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g C:\Users\DustinB\CodingClass\Inheritance\main.cpp -o C:\Users\DustinB\CodingClass\Inheritance\main.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\DustinB\AppData\Local\Temp\ccIpfZtM.o:C:\Users\DustinB\CodingClass\Inheritance/main.cpp:11: undefined reference to `Player::Player(std::basic_string_view<char, std::char_traits<char> >)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\DustinB\AppData\Local\Temp\ccIpfZtM.o: in function `main':
C:\Users\DustinB\CodingClass\Inheritance/main.cpp:12: undefined reference to `operator<<(std::ostream&, Player const&)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\DustinB\AppData\Local\Temp\ccIpfZtM.o: in function `Player::~Player()':
C:\Users\DustinB\CodingClass\Inheritance/player.h:6: undefined reference to `Person::~Person()'
collect2.exe: error: ld returned 1 exit status

Build finished with error(s).

 *  The terminal process terminated with exit code: -1. 

Any help would be much appreciated, I have struggled with this for two days.

Thanks!

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • Best way to learn C++ is by using a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Also, refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and then research"* and you'll find plenty of dupes for this. – Jason Sep 14 '22 at 13:34
  • 2
    Likely not compiling all *.cpp files. – sweenish Sep 14 '22 at 13:34
  • Compile all source files(.cpp) of your project. – Jason Sep 14 '22 at 13:39
  • and it's `out << "Player: [ game: " << player.m_game << " names: " << player.get_first_name() << " " << player.get_last_name() << "]";` `player.` not `Player.` – Martin Morterol Sep 14 '22 at 13:45
  • There are other design issues, but they appear to stem from not having learned about polymorphism yet. – sweenish Sep 14 '22 at 13:45
  • Thanks for noticing that! I fixed it but for some reason I still got the same error code. – Dustin Boatman Sep 14 '22 at 13:49
  • And you are correct, sweenish, I haven't learned much about polymorphism yet. – Dustin Boatman Sep 14 '22 at 13:52
  • How do you compile all source files? Would it not compile them all if I compile my main .cpp and they are included? – Dustin Boatman Sep 14 '22 at 13:55
  • ***C/C++: g++.exe build active file*** is likely the problem. The default VSCode builds only the active file. You need to edit your tasks.json to build all files. – drescherjm Sep 14 '22 at 13:59
  • Ahh, ok, I will look into that – Dustin Boatman Sep 14 '22 at 14:00
  • Here is a link to an answer of mine. I have answered a little more complicated question (as the problem had multiple source files in multiple folders) in the link but I think this should help as it explains the problem: [https://stackoverflow.com/questions/70856563/vscode-g-it-isnt-finding-cpp-definition-files/70856902#70856902](https://stackoverflow.com/questions/70856563/vscode-g-it-isnt-finding-cpp-definition-files/70856902#70856902) – drescherjm Sep 14 '22 at 14:01

0 Answers0