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!