I'm working on an inventory system for a game and I'm hitting a brick wall with object slicing; I'm losing variables on a reference to a derived class.
Below is an excerpt in which a T-shirt is created in the main game file, and then passed to a players inventory for storage. However only the variables present in the base class Item are preserved.
game.cpp
#include "Item.h"
#include "Clothes.h"
#include "Shirts.h"
shirt_item white_shirt = shirt_item(materialDescriptor::cotton, colourDescriptor::white);
player.getComponent<InventoryComponent>().storeItem(&whiteShirt);
InventoryComponent.cpp
bool InventoryComponent::storeItem(Item *inItem)
{
if (freeInvSpace() > 0)
{
items.push_back(inItem);
return true;
}
else if (freeInvSpace() < 0)
{
std::cout << "ERROR! Inventory over filled somehow" << std::endl;
}
return false;
}
InventoryComponent.h
#pragma once
#include "Components.h"
#include "Item.h"
#include "Clothes.h"
#include "Shirts.h"
class InventoryComponent : public Component // Entity component system
{
public:
std::vector<Item*> items;
InventoryComponent(int inSize)
{
size = inSize;
}
bool storeItem(Item *inItem);
...
}
Item.h
#pragma once
#include <string>
class Item
{
public:
std::string name,
description;
bool pronoun;
};
Clothes.h
#pragma once
#include <vector>
#include <string>
#include "Item.h"
#include "materialDescriptor.h"
#include "colourDescriptor.h"
class Clothes : public Item
{
public:
materialDescriptor material;
std::vector<bodyParts> coverage;
colourDescriptor colour;
Clothes(std::string inName, std::string inDescription, materialDescriptor inMaterial, colourDescriptor colour, bool inPronoun = false)
{
name = inName;
description = inDescription;
material = inMaterial;
pronoun = inPronoun;
}
Clothes() {}
};
Shirts.h
#pragma once
#include "Clothes.h"
#include "materialDescriptor.h"
#include "colourDescriptor.h"
class shirt_item : public Clothes
{
public:
shirt_item(materialDescriptor inMaterial, colourDescriptor inColour)
{
material = inMaterial;
colour = inColour;
description = "A basic shirt that covers the wearer from the elements";
name = "T-Shirt"
}
}
ECS.h
#pragma once
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
#include <bitset>
#include <array>
#include "Components.h"
class Component
{
public:
Entity* entity;
virtual void init() {}
virtual void update() {}
virtual void draw() {}
virtual ~Component() {}
private:
};
class Entity
{
private:
bool active = true;
std::vector<std::unique_ptr<Component>> components;
ComponentArray componentArray;
ComponentBitSet componentBitSet;
public:
template <typename T> T& getComponent() const
{
auto ptr(componentArray[getComponentTypeID<T>()]);
return *static_cast<T*>(ptr);
}
}
Using Vs2019 break points, the constructor for the T-shirt works but as soon as I attempt to use the object it is boiled down to it's base class: Item > Clothes > Shirts
If you pass and store inherited objects through pointers you eventually have to store them on the heap. Instead you are creating them on the stack. Just do
auto white_shirt = std::make_unique<shirt_item>(materialDescriptor::cotton, colourDescriptor::white);
Related
I am new to C++, and I am currently working on 2 different projects within the some solution in C++ with VSC and I am encountering the error "A class-qualified name is required"; I have created a custom class in a file inside a namespace like so in project Stock:
//in file Stock.h, project Stock
#pragma once
#include "CandleStick.h"
#include "StockRecord.h"
#include "MarketQue.h"
#include <string>
#include <vector>
namespace Stock {
class Stock {
private:
std::string s_stockID;
MarketQue* s_topCur;
StockRecord s_stockRecord;
std::vector<CandleStick> s_candleSticks;
unsigned int s_totalVol;
public:
Stock(const std::string&, MarketQue&, std::vector<unsigned int>&, unsigned int); //stockID, s_topCurPrice_B, s_topCurPrice_S, s_topCurVol_B, s_topCurVol_S, cs_interval, total_vol
void updateStock(int);
~Stock();
protected:
};
}
//in file Stock.cpp, project Stock
#include "Stock.h"
Stock::Stock::Stock(const std::string& stockID, MarketQue& topPrices, std::vector<unsigned int>& csInterval, unsigned int totalVol)
:s_stockID(stockID), s_topCur(&topPrices), s_stockRecord(StockRecord(topPrices.mq_size)), s_totalVol(totalVol) {
s_candleSticks.reserve(csInterval.size());
for (unsigned int i = 0; i < csInterval.size(); i++) {
s_candleSticks.emplace_back(topPrices.mq_topPrice_S[0], topPrices.mq_topPrice_B[0], csInterval[i]);
}
};
void Stock::Stock::updateStock(int totalVol) {
s_totalVol = totalVol;
s_stockRecord.updateStockRecord(*s_topCur);
for (unsigned int i = 0; i < s_candleSticks.size(); i++) {
s_candleSticks[i].updateCandleStick((s_topCur->mq_topPrice_B)[0], (s_topCur->mq_topPrice_S)[0]);
}
}
Stock::Stock::~Stock() {};
I have successfully linked the 2 projects together (I think, tested it in the main function with a file called App.cpp in project Market):
//in file App.cpp, project Market
#include "Stock.h"
#include "MarketQue.h"
#include <string>
#include <array>
#include <vector>
int main() {
//initializing
const std::string stockID = "001";
Stock::MarketQue topPrices(3);
std::vector<float> topCurPrice_B = { 3,2,1 };
std::vector<float> topCurPrice_S = { 4,5,6 };
topPrices.mq_topPrice_B = topCurPrice_B;
topPrices.mq_topPrice_S = topCurPrice_S;
std::vector<unsigned int> topCurVol_B = { 10,11,12 };
std::vector<unsigned int> topCurVol_S = { 13,14,15 };
topPrices.mq_topVol_B = topCurVol_B;
topPrices.mq_topVol_S = topCurVol_S;
std::vector<unsigned int> cs_Interval = { 100,300,1000 };
unsigned int totalVol= 3+2+1+4+5+6;
Stock::Stock first_stock(stockID, topPrices, cs_Interval, totalVol);
totalVol = 100;
first_stock.updateStock(totalVol);
//testing update
topCurPrice_B = { 13,12,11 };
topCurPrice_S = { 14,15,16 };
topCurVol_B = { 110,111,112 };
topCurVol_S = { 113,114,115 };
topPrices.mq_topPrice_B = topCurPrice_B;
topPrices.mq_topPrice_S = topCurPrice_S;
topPrices.mq_topVol_B = topCurVol_B;
topPrices.mq_topVol_S = topCurVol_S;
}
However, when I try to use this class(Stock::Stock) as a member inside of another class (in project Market), I received the error mentioned above ("A class qualified name is required");
I first did this:
//in file Market.h, project Market
#pragma once
#include "Stock.h"
#include <string>
#include <vector>
namespace Market {
class Market {
private:
std::string marketID;
std::vector<Stock::Stock> stock; //received error
Stock::Stock stock;
public:
Market();
void updateStock();
~Market();
protected:
};
}
At this point i figured it might be because of the namespace Market making it Market::Stock::Stock and i decided to remove the namespace Market:
//in file Market.h, in project Market
#pragma once
#include "Stock.h"
#include <string>
#include <vector>
class Market {
private:
std::string marketID;
std::vector<Stock::Stock> stock;
public:
Market();
void updateStock();
~Market();
protected:
};
I'd like to ask how to fix this problem, and if possible can someone explain to me what is happenning to the code that caused the error?
Thank you very much for your time.
I have a class Project and each Project can have different tasks.
Project.h:
#pragma once
#include "Task.h"
#include <vector>
using namespace std;
class Project
{
private:
vector<Task> Tasks;
public:
Project::Project(int inleesgetal);//constructor
vector<Task> GetTasks();
};
Project.cpp:
#include "Project.h"
#include <string>
#include <vector>
Project::Project(int inleesgetal)
{
//constructor
Tasks.resize(Numbertasks);
}
vector<Task> Project::GetTasks()
{
return Tasks;
}
Task.h:
#pragma once
#include <vector>
using namespace std;
class Task
{
private:
//Info:
int StartTime_Solo;
public:
Task(); //constructor
void SetStartTime_Solo(int st_s);
int GetStartTime_Solo();
};
Task.cpp:
#include "Task.h"
#include <string>
#include <vector>
#include <iostream>
using namespace std;
Task::Task()
{
//constructor
StartTime_Solo = 0;
}
int Task::GetStartTime_Solo()
{
return StartTime_Solo;
}
void Task::SetStartTime_Solo(int st_s)
{
StartTime_Solo = st_s;
}
main:
#include <iostream>
#include <vector>
#include "Task.h"
#include "Project.h"
using namespace std;
int main()
{
Project Project1(6);
Project1.GetTasks()[2].SetStartTime_Solo(55);
cout << "test:" << Project1.GetTasks()[2].GetStartTime_Solo();
return 0;
}
Now when I try to set the 3rd task of Project1 to a starttime of 55 and then print the start time out it still gives me 0 as a result.
Why is this? And how can I change my code so it actually sets the starttime to 55?
vector<Task> GetTasks();
should be
const vector<Task>& GetTasks() const;
vector<Task>& GetTasks();
And so with definitions:
vector<Task> Project::GetTasks()
{
return Tasks;
}
should be:
const vector<Task>& Project::GetTasks() const { return Tasks; }
vector<Task>& Project::GetTasks() { return Tasks; }
The problem is that you are returning a copy of the vector<Task> from the GetTasks function. You then modify this copy and throw it away right afterwards. The internal member of Project is not changed.
If you return by reference like this:
vector<Task>& GetTasks();
Then you are basically returning something that points to the internal vector, and so when you modify it, you actually modify the member data of your class.
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 want to pass my vector "myStaffs" from Team Class by reference to Manage Class because I want to manipulate the data of staffs and sort them. How do I pass it by reference?
Header Staff
#ifndef STAFF_H
#define STAFF_H
#include <vector>
#include <cstdlib>
#include <ctime>
#include <string>
class Staff
{
public:
Staff();
Staff(int, int, int, int);
~Staff();
static int genRanNum(int);
static Staff* createStaff(int);
const int getSize();
private:
int staSkills1,staSkills2,staSkills3;
int staId;
//int staDeptAsigned;
//std::string staName;
};
#endif
CPP Staff
#include "Staff.h"
#include <iostream>
using namespace std;
Staff::Staff():
staId(0),
staSkills1(0),
staSkills2(0),
staSkills3(0)
{
}
Staff::Staff(int id, int s1, int s2, int s3):
staId(id),
staSkills1(s1),
staSkills2(s2),
staSkills3(s3)
{
}
Staff *Staff::createStaff(int s)
{
Staff *staff = new Staff();
staff->staId = s;
staff->staSkills1 = genRanNum(10);
staff->staSkills2 = genRanNum(10);
staff->staSkills3 = genRanNum(10);
return staff;
}
int Staff::genRanNum(int num)
{
return 1+(rand()%num);
}
Staff::~Staff()
{
}
Header Team
#ifndef TEAM_H
#define TEAM_H
#include "Staff.h"
#include <vector>
#include <iostream>
using std::vector;
class Team: public Staff
{
public:
Team();
~Team();
private:
vector<Staff *> myStaffs;
};
#endif // TEAM_H
CPP Team
#include "Team.h"
const int SIZE = 30;
Team::Team():
myStaffs(SIZE)
{
for(int iStaff = 0; iStaff <= SIZE; iStaff++)
{
myStaffs[iStaff] = createStaff(iStaff);
}
}
Team::~Team()
{
}
Header Manage
#ifndef OPTIONS_H
#define OPTIONS_H
#include "Team.h"
#include <vector>
#include <iostream>
using std::vector;
class Manage
{
public:
Manage();
~Manage();
private:
// vector
};
CPP Manage
#include "Manage.h"
Manage::Manage()
{
}
Manage::~Manage()
{
}
#endif
Its simple you pass it as you would pass any other object by reference
int sortEmployee(std::vect<Staff *> &staffList> {
// ... code to sort Employee
}
and you can call it like below
vector<Staff *> myStaffs
result = sortEmployee(myStaffs);
It's as simple as
#include <vector>
void myVectorManglingFun(std::vector<Staff *> &myStaff) //notice the &
{
//do something here
}
If you don't need to modify the vector, then always use a const reference.
void myVectorReadingFun(const std::vector<Staff *> &myStaff)
Dialog.h
#include "WBasic.h"
#include "WButton.h"
#include "WData.h"
#ifndef WDIALOG_H_INCLUDED
#define WDIALOG_H_INCLUDED
class WDialog : public WBasic
{
private:
WButton wB;
WData wD;
public:
//Constructor
WDialog(const int& e = 0, const WButton& = WButton(0,0), const WData& = WData(0,0,0));
~WDialog();
};
#endif // WDIALOG_H_INCLUDED
Dialog.cpp
#include <iostream>
#include "WDialog.h"
WDialog::WDialog(const int& e, const WButton& WBUTTON, const WData& WDATA) :
WBasic(e), wB(WBUTTON), wD(WDATA)
{
}
The code above works great, however I'm trying to make "WButton wB" a vector changing it to"WButton wB[3];"
class WDialog : public WBasic
{
private:
WButton wB[3];
WData wD;
};
But then I've no idea how deal with the Constructor.
You can use vector to solve this problem.
I have written a small example below.
#include <iostream>
#include <vector>
using namespace std;
class A{
};
class B{
public:
B():vec (4,A())
{
}
private :
vector<A> vec;
};
int main() {
// your code goes here
B obj();
return 0;
}
You can observe how I have initialized vector vec with three class A object.
In my opinion if you can (your compiler support C++11) prefer std::array
#include <array>
std::array<WButton, 3> wB;
Then in your contructor use an initializer list:
WBasic(e),
wB{WButton(...), WButton(...), WButton(...)},
wD(WDATA)