I'm having this issue on another program, but I tried to simplify it with this one. I cannot set the weapon name through p.getWeaopn().setName("sword"); It works fine when I simply set it through its own object, but when I try to access the setter through player it doesn't set anything.
#include <iostream>
#include <string>
#include "Player.h"
#include "Weapon.h"
using namespace std;
int main()
{
Player p; // Player contains only a Weapon weapon;
Weapon w; // Weapon only contains a string name;
//w.setName("sword"); // this changes the name of the weapon
p.setWeapon(w);
p.weapon.setName("sword"); // this also changes the name
p.getWeapon().setName("sword"); // this is not setting the name. Why?
// checking if weapon has a name
if (p.getWeapon().getName().empty())
{
cout << "Weapon name is empty!" << endl;
}
else
{
cout << "Weapon name is " << p.getWeapon().getName() << endl;
}
}
Weapon.h
#pragma once
#include <string>
using namespace std;
class Weapon
{
private:
string name;
public:
string getName();
void setName(string);
};
Weapon.cpp
#include "Weapon.h"
string Weapon::getName()
{
return name;
}
void Weapon::setName(string n)
{
name = n;
}
Player.h
#pragma once
#include "Weapon.h"
class Player
{
private:
Weapon weapon;
public:
Weapon getWeapon();
void setWeapon(Weapon);
};
Player.cpp
#include "Player.h"
Weapon Player::getWeapon()
{
return weapon;
}
void Player::setWeapon(Weapon w)
{
weapon = w;
}
Weapon Player::getWeapon()
You return a copy and not a reference of the weapon, so any change to the copy does not affect the original.
For return a reference, use & operator:
Weapon& Player::getWeapon()
{
return this->weapon;
}
Player::getWeapon() returns a copy of the weapon every time instead of a reference to the weapon. Changing the name in the copy changes nothing in the original.
Related
I am coding a little RPG(Role Playing Game)
Here is the situation: I created an object Personnage.
In my classes, I created a method atttaquer. But I would like that after calling my method attaquer it writes something like this: Goliath attaque David . But to that, I need to grab the name of the Object. Because the player may want to edit the name of Object (The personage name) before playing.
There is my code:
Personnage.h
#ifndef Personnage_h
#define Personnage_h
#include <string>
#include "Arme.h"
class Personnage{
//methods
public:
Personnage();
Personnage(std::string nomArme, int degatsArme);
Personnage(int vie, int mana);
// ~Personnage();
void recevoirDegats(int nbDegats);
void attaquer(Personnage &cible);
private:
// Attributs
int m_vie;
int m_magie;
std::string m_nom;
};
#endif
My Personnage.cpp code:
#include "Personnage.h"
#include <string>
#include <iostream>
void Personnage::recevoirDegats(int nbDegats){
m_vie -= nbDegats;
if (m_vie < 0) {
m_vie = 0;
}
}
void Personnage::attaquer(Personnage &cible){
cible.recevoirDegats(m_arme.getDegats());
// if David attacks Goliath I want to write std::cout << David << "attaque "<< Goliath << endl; but I do not know how to grab the name of the object after it's creation
}
There is my main.cpp
#include <iostream>
#include <string>
#include "Personnage.h"
//#include "Personnage.cpp"
#include <ctime>
using namespace std;
int main()
{
Personnage David, Goliath, Atangana("Ak47", 35);
Goliath.attaquer(David);
return 0;
}
If you want to give your objects names, it cannot be the variable names. They are only meant for the compiler and they are fixed. So you need to create a class that can have a name:
class NamedObject
{
private:
std::string m_name;
public:
const std::string& getName() const
{
return m_name;
}
void setName(const std::string& name)
{
m_name = name;
}
}
And if you want your classes to have a name, the easiest way would be to derive from it:
class Personnage : NamedObject {
Then you can say:
Personnage player1, player2;
player1.setName("David");
player2.setName("Goliath");
Alternatively, you can get those string from user input.
And if you need to address one by name:
std::cout << player1.getName() << " please make your move." << std::endl;
I don't understand this error
here is a link to view the code online:
https://onlinegdb.com/rkirYvU_M
I am trying to add the names of the drivers, owners, and model to vectors, and we need to use pointers and files.
Here is my main file:
#include "person.h"
#include "car.h"
#include <iostream>
#include <vector>
std::vector <Person*>people;
std::vector <Car*> cars;
int main()
{
bool done = false;
Person person;
while(! done)
{
std::cout << "\n Please enter the owners ";
Person*prompt_info();
std::cout << "\n Please enter the drivers ";
Car*prompt_info();
Car*set();
Car*print();
}
return 0;
}
Here is the person.h file:
#ifndef PERSON_H
#define PERSON_H
#include <string>
#include <iostream>
//using namespace std;
class Person
{
public:
Person();
std::string get_name();
int get_age();
void prompt_info();
private:
std::string name;
int age;
};
#endif
Here is the person.c++ file:
#include "person.h"
Person::Person()
{
}
void Person::prompt_info()
{
std::cout << " name: ";
std::cin >> name;
std::cout << "enter their age: ";
std::cin >> age;
}
std::string Person::get_name()
{
return name;
}
int Person::get_age()
{
return age;
}
Here is the car.h file:
#ifndef CAR_H
#define CAR_H
#include <string>
#include <iostream>
#include "person.h"
using namespace std;
class Car
{
public:
Car();
std::string get_model();
Person* get_owner();
Person* get_driver();
void print();
void set(Person _owner,Person get_driver);
void prompt_info();
private:
std::string model;
Person* owner;
Person* driver;
};
#endif
I am trying to understand this error.
main.cpp:23:25: error: ambiguating new declaration of 'Car* prompt_info()'
Car*prompt_info();
^
You seem to be confusing function declarations with member functions. Just declare a Person object on the stack and call the method through it's object. Do the same for your Car object. You can use your objects like this.
while(! done)
{
Person person; ///< Person object named 'person'
Car car; ///< Car object named 'car'
std::cout << "\n Please enter the owners ";
person.prompt_info();
std::cout << "\n Please enter the drivers ";
car.prompt_info();
car.set();
car.print();
// TODO do something with your objects (store to vector?)
// next time through the loop your person and car will
// get initialized all over again
}
return 0;
You will have to store your temporary objects before they go out of scope if you want to use them later.
Each time I've attempted to build my project, I receive the same error:
>error: no matching function for call to 'Agent::Agent()'
>note: candidates are: Agent::Agent(std::string, room*)
>note: Agent::Agent(const Agent&)
Initially I assumed that I was feeding the wrong values, but even after seemingly correcting, I still get the same error.
main
#include <iostream>
#include <string>
#include <sstream>
#include "room.h"
#include "Thing.h"
//#include "Agent.h"
//#include "Grue.h"
//#include "Player.h"
using namespace std;
int main()
{
srand(time(NULL));
room *entrance = new room("Entrance","A wide open entrance...", 100);
room *hallway = new room("Hallway","A long hallway...", 50);
room *ballroom = new room("Ballroom","A huge ballroom...", 200);
room *garden = new room("Garden","A lush garden...", 150);
entrance->link("south", hallway);
hallway->link("north", entrance);
hallway->link("east", ballroom);
ballroom->link("west", hallway);
ballroom->link("east", garden);
hallway->printLinked();
while(true)
{
for(int i = 0; i < agents.size(); i++)
{
bool ok = agents[i]->act();
if(!ok)
{
cout << "Game quits." << endl;
return 0;
}
}
}
Player *josh = new Player("Josh", entrance);
Player *tracy = new Player("Tracy", entrance);
game.addAgent(josh);
game.addAgent(tracy);
cout << "Welcome!" << endl;
// the step() function in the Game class will eventually
// return false, when a player chooses to quit;
// this tiny "while" loop keeps asking the game.step()
// function if it is false or true; the effect is
// that the step() function is called repeatedly
// until it returns false
while(game.step());
return 0;
}
Thing header
#ifndef THING_H
#define THING_H
#include <iostream>
#include <string>
#include "room.h"
class room;
class Thing
{
private:
std::string name, desc;
protected:
room* cur_room;
public:
Thing(std::string _name, std::string _desc);
std::string getName();
std::string getDesc();
int getSize();
};
#endif // THING_H
Thing cpp
#include "Thing.h"
Thing::Thing(std::string _name, std::string _desc)
{
name = _name;
desc = _desc;
cur_room = NULL;
}
std::string Thing::getName()
{
return name;
}
std::string Thing::getDesc()
{
return desc;
}
int Thing::getSize()
{
return size;
}
Agent header
#ifndef AGENT_H
#define AGENT_H
#include "Thing.h"
#include <iostream>
#include <string>
#include "room.h"
class Agent : public Thing
{
protected:
//bool walk(std::string exit);
room *cur_room;
std::string name;
public:
Agent(std::string _name, room *_cur_room);
void get_curroom();
virtual bool act() = 0;
std::string getName() { return name; }
};
#endif // AGENT_H
Agent cpp
#include "Agent.h"
Agent::Agent(std::string _name, room *_cur_room)
{
name = _name;
room = _cur_room;
}
bool Agent::walk(std::string exit)
{
return 0;
}
bool Agent::act()
{
return 0;
}
void Agent::get_curroom()
{
return cur_room;
}
Player header
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include <string>
#include "Agent.h"
#include "room.h"
class room;
class Player : public Agent
{
private:
std::string name;
protected:
public:
Player(std::string _name, room *starting_room);
bool Act();
};
#endif // PLAYER_H
Player cpp
#include "Player.h"
Player::Player(std::string _name, room *starting_room)
{
name = _name;
cur_room = starting_room;
}
bool Player::Act()
{
std::cout << "Where do you want to go? (or 'quit')" << std::endl;
}
I'm honestly stumped on where to go next. Any help is greatly appreciated!
In your inheritance chain of Player<-Agent<-Thing, you aren't passing the constructor parameters up to the parent. So, you need to change the Player constructor to:
Player::Player(std::string _name, room *starting_room)
:Agent(_name, starting_room)
{
name = _name;
cur_room = starting_room;
}
And you need to change the Agent constructor to:
Agent::Agent(std::string _name, room *_cur_room)
:Thing(_name, "")
{
name = _name;
room = _cur_room;
}
The parts I added after the colons are called initialization lists. One use of these is to call the constructor of the parent class. In C++, you have to call the parent class's constructor by name, since there is no keyword to reference the parent class generally.
The problem is that in the constructor of Player you don't call the constructor of the base class Agent and the latter doesn't have the default constructor. To fix it call the Agent's constructor (or add the default constructor):
Player::Player(std::string _name, room *starting_room)
: Agent(_name, starting_room)
{
// ...
}
I have a C++ project in Visual Studio 2015.
GameManager.h and Input.h both give me a syntax error: identifier 'Player'. This happens because I want to give an object of type 'Player' as an argument to functions in these two Header files and their appropriate C++ Files.
How do I fix that? For further information I have provided my code.
main.cpp:
#include "GameManager.h"
#include "Input.h"
#include "Player"
#include <iostream>
#include <string>
using namespace std;
const int maxPlayerCnt = 10;
static Player p1, p2, morePlayers[maxPlayerCnt];
int main()
{
GameManager game;
game.Game(p1, p2, morePlayers);
return 0;
}
It creates an object of type GameManager and 3 objects of type Player.
GameManager.h:
#include "Player.h"
class GameManager
{
public:
void Game(Player p1, Player p2, Player morePlayers[]);
};
GameManager.cpp:
#include "GameManager.h"
void GameManager::Game(Player p1, Player p2, Player morePlayers[])
{
int playerCnt = 0;
Input input;
input.getPlayerDetails(playerCnt, p2);
input.getMorePlayerDetails(playerCnt, morePlayers);
}
It creates an object of type Input to use further functions and will get more code, once I figure this problem out. And then calls to functions with specific arguments it gets from main.cpp
Input.h:
#pragma once
#include "Player.h"
#include <iostream>
#include <string>
using namespace std;
class Input
{
public:
Input();
void getPlayerDetails(int &playerNum, Player p);
void getMorePlayerDetails(int &playerNum, Player p[]);
};
It includes everything Input.cpp needs and initializes the funcitons
Input.cpp:
#include "Input.h"
void Input::getPlayerDetails(int &playerNum, Player p)
{
playerNum++;
string currentName;
char currentSymbol;
cout << "Player " << playerNum << ", what is your name?\n";
cin >> currentName;
p.setName(currentName);
cout << currentName << " what is your symbol?\n";
cin >> currentSymbol;
p.setSymbol(currentSymbol);
}
void Input::getMorePlayerDetails(int &playerNum, Player p[])
{
int plNum = playerNum;
if (playerNum >= 12)
cout << "You can't get another player!\n";
else
{
//getPlayerDetails(p[playerNum - 2], (plNum - 2));
}
}
It for now has all the functions needed and both get an object of type Player. And the second function is not quite done now. But that is not important.
Player.h:
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Player
{
private:
string _name;
char _symbol;
public:
Player();
void getName();
void setName(string name);
void setSymbol(char symbol);
};
Player.cpp:
#include "Player.h"
Player::Player()
{
}
void Player::getName()
{
cout << "I have no name!\n";
}
void Player::setName(string name)
{
_name = name;
}
void Player::setSymbol(char symbol)
{
_symbol = symbol;
}
If you can help me, I would be pleased to see your response.
#include "player.h"
class team
{
public:
team();
void addPlayer(player);
void deletePlayer(int);
double getTotalCost();
int getPlayerCount();
double inBank();
string toString();
private:
player a;
int playerId;
double bank;
int i;
};
#include "../../std_lib_facilities.h"
#include "team.h"
team::team()
{
vector <player> a;
player a;
}
team::addPlayer(player)
{
a.push_back(a);
}
If more info is needed please ask. Thank you in advance for any help.
I assume this is what you meant:
#include "player.h"
#include <vector>
class team
{
public:
team();
void addPlayer(player);
void deletePlayer(int);
double getTotalCost();
int getPlayerCount();
double inBank();
string toString();
private:
vector<player> a;
int playerId;
double bank;
int i;
};
#include "../../std_lib_facilities.h"
#include "team.h"
team::team()
{
}
team::addPlayer(player p)
{
a.push_back(p);
}
You shoud have your vector variable a member of your class or create it on heap a keep the pointer to it. Now you are creating your vector on a stack in teem constructor. It will deleted when constructor is finished.
Also you can't use name a for both player and vector. I recommend you to read some C++ books first.
There's so much wrong with this I don't know where to begin, you declare a SINGLE player, here:
private:
player a; // why not just call every variable in your program "a"?
And then in your constructor of the team:
team::team()
{
vector<player> a; // a vector that will be destroyed on exit from constructor
player a; // another single player, but you've just defined 'a' so you should get a compiler error along the lines of redefinition.
}
I suspect you want something like:
#include <vector>
#include <string>
#include "player.h"
class team
{
private:
std::vector<player> m_Players; // players
public:
void addPlayer(const player& _player) { m_Players.push_back(_player); }
}; // eo class team
What do u need ? Store players in your class team ?
#include <iostream>
#include <vector>
using namespace std;
class Player
{
public:
Player(int id) { m_id = id; }
int GetId() {return m_id; }
private:
int m_id;
};
class team
{
public:
team(){};
void AddPlayer(Player p) {m_arr.push_back(p);}
size_t Size(){ return m_arr.size();}
Player GetPlayer(size_t index) {return m_arr[index];}
private:
vector<Player> m_arr;
};
void main()
{
team t;
for (int i =0; i < 10; ++i)
{
t.AddPlayer(Player(i));
}
for (int i =0; i < t.Size();++i)
{
cout << "Player [" << i + 1 << "] with id: " << t.GetPlayer(i).GetId() << endl;
}
}