Ciao,
sto facendo un test per la creazione di 2 giocatori in un programmino che sto scrivendo.
vi posto il sorgente.
main.cpp
#include <iostream>
#include "createPlayer.h"
int main()
{
createPlayer();
}
Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
class Player
{
public:
Player(int ap);
Player(int h, int s, int ap);
void setName(std::string n);
void setHealth(int h);
void setShield(int s);
void setAttackPower(int ap);
std::string getName() const;
int getHealth() const;
int getShield() const;
int getAttackPower() const;
protected:
std::string name;
int health;
int shield;
int attackPower;
};
#endif // PLAYER_H
Player.cpp
#include "..\include\Player.h"
#include <string>
Player::Player(int ap)
: health(100), shield(100), attackPower(ap)
{
std::cout << "Enter player's name: ";
getline(std::cin, name);
}
Player::Player(int h, int s, int ap)
: health(h), shield(s), attackPower(ap)
{
std::cout << "Enter player's name: ";
getline(std::cin, name);
}
void Player::setName(std::string n) { name.assign(n); }
void Player::setHealth(int h) { health = h; }
void Player::setShield(int s) { shield = s; }
void Player::setAttackPower(int ap) { attackPower = ap; }
std::string Player::getName() const { return name; }
int Player::getHealth() const { return health; }
int Player::getShield() const { return shield; }
int Player::getAttackPower() const { return attackPower; }
createPlayer.h
#ifndef CREATEPLAYER_H
#define CREATEPLAYER_H
void createPlayer();
#endif // CREATEPLAYER_H
createPlayer.cpp
#include <iostream>
#include <fstream>
#include "createPlayer.h"
#include "include\Player.h"
void createPlayer()
{
Player po(50);
int counter = 0;
std::ofstream toFile("save.txt");
while (counter++ < 2)
{
toFile << po.getName() << po.getHealth() << po.getShield;
}
il problema è nel main.cpp.
undefined reference to createPlayer()...
come mai? ho incluso l'header...