Вот мой код:
main. cpp
#include <iostream>
#include "Player.hpp"
void display_active_players() {
std::cout << "Active players: " << Player::get_num_players() << std::endl;
}
int main() {
display_active_players();
Player hero {"Hero"};
display_active_players();
{
Player frank {"Frank"};
display_active_players();
}
display_active_players();
Player *enemy = new Player {"Enemy", 100, 100};
display_active_players();
delete enemy;
display_active_players();
std::cout << std::endl;
return 0;
}
Player. cpp
#include "Player.hpp"
int Player::num_players {0};
Player::Player(std::string name_val, int health_val, int xp_val)
: name {name_val}, health {health_val}, xp {xp_val} {
num_players++;
}
Player::Player(const Player &source)
: Player {source.name, source.health, source.xp} {
}
Player::~Player() {
num_players--;
}
int Player::get_num_players() {
return num_players;
}
Player.hpp
#ifndef PLAYER_H
#define PLAYER_H
#include <string>
class Player {
private:
static int num_players;
std::string name;
int health;
int xp;
public:
std::string get_name() { return name; }
int get_health() { return health; }
int get_xp() { return xp; }
Player(std::string name_val = "None", int health_val = 0, int xp_val = 0);
// Copy constructor
Player(const Player &source);
// Destructor
~Player();
static int get_num_players();
};
#endif
Ошибка при попытке компиляции:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x1e): undefined reference to `Player::get_num_players()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0xaa): undefined reference to `Player::Player(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x10e): undefined reference to `Player::Player(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x137): undefined reference to `Player::~Player()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x18c): undefined reference to `Player::Player(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x1b9): undefined reference to `Player::~Player()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x1f4): undefined reference to `Player::~Player()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x24a): undefined reference to `Player::~Player()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x28d): undefined reference to `Player::~Player()'
collect2.exe: hata: ld ��k�� durumu 1 ile d�nd�