Erase from vector - c++

I have these classes:
class PC
{
public:
PC(string in_operatingSystem,int in_ramSlots,int in_pcieSlots,int in_totalRamSlots, int in_gbPerRam, int in_cpu, int in_ssd, int in_cost);
virtual void Print() = 0;
virtual void Upgrade() = 0;
protected:
string operatingSystem;
int ramSlots,pcieSlots,totalRamSlots,gbPerRam;
int cpu,ssd;
int cost;
};
class HomePC: public PC
{
public:
HomePC(string in_operatingSystem,int in_ramSlots,int in_pcieSlots,int in_totalRamSlots, int in_gbPerRam, int in_cpu, int in_ssd, int in_cost, string in_model);
void Print();
void Upgrade(){};
private:
string model;
};
and I'm saving the content into the vectors like this:
PCList.push_back(new HomePC("MacOS",2,0,1,4,2,256,800,Model));
I'm trying to find a way to find a specific Model(for example UserPC) in vector and erase this PC from my list.

You can add a getter for model
class HomePC: public PC
{
public:
HomePC(string in_operatingSystem,int in_ramSlots,int in_pcieSlots,int in_totalRamSlots, int in_gbPerRam, int in_cpu, int in_ssd, int in_cost, string in_model);
void Print();
void Upgrade(){};
std::string getModel() const { return model; }
private:
string model;
};
and dynamic cast each element:
for (auto pcIt = PCList.cbegin(); pc != PCList.cend(); ++pc) {
const auto * const homePc = dynamic_cast<HomePC *>(*pcIt);
if (homePc && homePc->getModel() == "UserPC") {
PCList.erase(pcIt);
break;
}
}
You have to add a virtual destructor to PC otherwise you can't dynamic cast.

Otherwise erasing, you can try different ways
std::vector<std::unique_ptr<HomePC>> pileTemp;
std::vector<std::unique_ptr<HomePC>> pileTarget;
std::unique_ptr<HomePC> p1(new HomePC("Windows",2,0,1,4,2,256,800,"UserPC"));
pileTemp.push_back(std::move(p1));
std::unique_ptr<HomePC> p2(new HomePC("MacOs",2,0,1,4,2,256,800,"PCUser"));
pileTemp.push_back(std::move(p2));
std::for_each(pileTemp.begin(), pileTemp.end(), [&](std::unique_ptr<HomePC>& p)->void{ if(p->getmodel() != "UserPC" ) {
pileTarget.push_back(std::move(p));
}
});
Then, you can clear the temporary container.

Related

Convert c++98 code to new c++17 code when using std::vector of pointers

i dont what to manage or less as i can manage raw pointers note in the example i have to delete the object before removeing it from the vector i want to avoid it it here and later
what will be good case to convert this code to using unique_ptr or shared_ptr
class GameState
{
public:
virtual bool onEnter() = 0;
virtual bool onExit() = 0;
virtual std::string getStateID() const = 0;
};
class MenuState : GameState
{
public:
MenuState(){};
virtual ~MenuState(){};
bool onEnter(){};
bool onExit(){};
std::string getStateID() const;
private:
static const std::string s_menuId;
};
class StateMechine
{
public:
void pushState(GameState* pState)
{
m_gameStates.pop_back(pState);
m_gameStates.back()->onEnter();
}
void changeState(GameState* pState)
{
if(!m_gameStates.empty())
{
if(m_gameStates.back()->onExit())
{
delete m_gameStates.back();
m_gameStates.pop_back();
}
}
}
private:
std::vector<GameState*> m_gameStates;
}
int main(int argc ,char** argv)
{
GameState *gs = new MenuState();
StateMechine sm;
sm.pushState(gs);
sm.changeState(gs);
}
The std::vector<GameState*> can be replaced with a std::vector<std::unique_ptr<GameState>>. That way a call to m_gameStates.pop_back() will delete the corresponding object.
class StateMechine
{
public:
void pushState(std::unique_ptr<GameState> pState)
{
m_gameStates.push_back(std::move(pState));
m_gameStates.back()->onEnter();
}
void changeState()
{
if(!m_gameStates.empty())
{
if(m_gameStates.back()->onExit())
{
m_gameStates.pop_back();
}
}
}
private:
std::vector<std::unique_ptr<GameState>> m_gameStates;
};
int main(int argc ,char** argv)
{
StateMechine sm;
sm.pushState(std::make_unique<MenuState>());
sm.changeState();
}

Issue with polymorphism with smart pointers and vectors c++

I am currently trying to learn c++ and I am having an issue when trying to create a vector which iterates through several different objects whom all inherit from the same base class with smart pointers.
I parse a file and create the objects and insert them into the vector depending on the character parsed but I keep getting the error:
Error C2664 'std::unique_ptr<Test *,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': cannot convert argument 1 from 'std::unique_ptr<Test2,std::default_delete<_Ty>>' to 'std::nullptr_t'"
Code is as follows:
class Test {
public:
virtual ~Test(){}
virtual int update() {}
};
class Test2 : public Test {
private:
int a;
public:
Test2() {
}
Test2(int n) {
a = n;
}
int update() override {
return a;
}
};
class Test3 : public Test {
private:
int a;
public:
Test3() {
}
Test3(int n) {
a = n;
}
int update() override {
return a;
}
};
class Test4 : public Test {
private:
int a;
public:
Test4() {
}
Test4(int n) {
a = n;
}
int update() override {
return a;
}
};
class manager {
private:
std::vector<std::unique_ptr<Test*>> vList;
std::ifstream lvlFile;
public:
std::string tmp;
manager() {
}
~manager() {
}
void init(const char *path) {
lvlFile.open(path, 0);
while (lvlFile.eof() != true) {
std::getline(lvlFile, tmp);
for (char& a : tmp) {
switch (a) {
case 'w':
vList.emplace_back(std::make_unique<Test2>(2));
break;
case 'g':
vList.emplace_back(std::make_unique<Test3>(3));
break;
}
}
}
}
void print() {
for (auto& i : vList) {
std::cout << (*i)->update() << std::endl;
}
}
};
manager *m;
int main() {
m = new manager();
m->init("lvl.txt");
_getch();
}
Maybe I have misunderstood something crucial here but I have been looking around and found no real answers so any pointers to this would be most welcome!
Change std::vector<std::unique_ptr<Test*>> to std::vector<std::unique_ptr<Test>>.
std::unique_ptr<Test*> is a pointer to pointer (Test**).

Virtual Class and Inheritance

I get a compile error "cannot convert 'Chips' to 'GroceryItem' and I'm having trouble figuring out how to actually convert something to a virtual class object. Teacher provided a 'GroceryCart' class that adds GroceryItems to the cart in 'main' and prints the cart out.
class GroceryItem {
public:
virtual ~GroceryItem();
virtual float GetPrice() = 0;
virtual char* GetDescription() = 0;
virtual float GetWeight() = 0;
};
#include "GroceryItem.h"
class Chips : public GroceryItem {
public:
Chips();
~Chips();
float GetPrice();
char* GetDescription();
float GetWeight();
private:
float price;
char* itemName;
float weight;
};
#include "GroceryItem.h"
#include "Chips.h"
Chips::Chips() {
price = 0.75;
itemName = new char[5];
itemName = "Chips";
weight = 1.0;
}
Chips::~Chips() {
delete this;
}
float Chips::GetPrice() {
return price;
}
char* Chips::GetDescription() {
return itemName;
}
float Chips::GetWeight() {
return weight;
}
int main(int argc, const char * argv[])
{
GroceryCart c;
c.AddItemToCart(new Chips);
std::cout << c;
return 0;
}
void GroceryCart::AddItemToCart(GroceryItem *i)
{
items.push_back(i);
}
provide full error log, check that container parameterized with pointer to GroceryItem (<GroceryItem*>)
As someone pointed out
delete[] itemName; //destructor
but you also have to define non-trivial copy-constructor/operator=, so it's better to replace char* with std::string, also need defined destructor for base abstract class even it's pure virtual.

Class factory to create derived classes c++

I'm currently learning about class factory patterns with C++. I keep having errors while trying to implement the factory. Suppose I have an abstract class and two derived classes. What I want the factory to do is to create a new object of the base class like so: Ball *sc = new SoccerBall();
I am not sure on how to implement this, I have tried but of no avail. What do I need to fix?
class Ball
{
public:
Ball();
virtual ~Ball();
virtual int getSize() const = 0;
virtual void setBallSize(int s) = 0;
virtual string ballManufacturer() const = 0;
protected:
int ballSize;
}
class Soccerball:public Ball
{
public:
Soccerball();
Soccerball(int size);
~Soccerball();
int getSize() const;
void setBallSize(int s);
string ballManufacturer() const;
}
class Soccerball:public Ball
{
public:
Soccerball();
Soccerball(int size);
~Soccerball();
int getSize() const;
void setBallSize(int s);
string ballManufacturer() const;
}
class Basketball:public Ball
{
public:
Basketball();
Basketball(int size);
~Basketball();
int getSize() const;
void setBallSize(int s);
string ballManufacturer() const;
}
class BallFactory
{
public:
Ball* createBall(string s)
{
if(s == "Soccer")
{
return new Soccerball(5);
}
if(s == "Basket")
{
return new Basketball(6);
}
}
}
This how your code will work, but above when you are posting a question you should provide "Short Self Contained Correct Code" and make easy for people to understand your problem easily.
#include <iostream>
using namespace std;
class Ball
{
public:
Ball()
{
cout<<"Ball ctr"<<endl;
}
virtual ~Ball()
{
}
virtual int getSize() const = 0;
virtual void setBallSize(int s) = 0;
virtual string ballManufacturer() const = 0;
protected:
int ballSize;
};
class Soccerball:public Ball
{
public:
Soccerball()
{
cout<<"create Default Soccer Ball "<<endl;
}
Soccerball(int size)
{
cout<<"create Soccer Ball "<<size<<endl;
}
~Soccerball()
{
}
int getSize() const
{
return ballSize;
}
void setBallSize(int s)
{
ballSize = s;
}
string ballManufacturer() const
{
return "";
}
};
class Basketball:public Ball
{
public:
Basketball()
{
cout<<"create default Baseket Ball "<<endl;
}
Basketball(int size)
{
cout<<"create Baseket Ball "<<size<<endl;
}
~Basketball()
{
}
int getSize() const
{
return ballSize;
}
void setBallSize(int s)
{
ballSize = s;
}
string ballManufacturer() const
{
return "";
}
};
class BallFactory
{
public:
//Factory method
static Ball* createBall(string s)
{
if(s == "Soccer")
{
return new Soccerball(5);
}
if(s == "Basket")
{
return new Basketball(6);
}
}
};
int main()
{
Ball* ptr = BallFactory::createBall("Soccer");
return 0;
}
But you also need to understand how Factory design pattern works and how a namesake virtual constructor is created and why you would use a parameterized factory. Or could you use a template factory.

C++ Pass a argument through a class with array index?

class cat
{public:
void dog(int ID, char *value) // int ID I'd like to be the index array it was called from?
{
debug(ID, value);
}
}
cat cats[18];
cats[1].dog("value second arg, first arg auto filled from index array");
I want something similar to this.
include <vector>
class CatArray;
class Cat {
// This line means that the CatArray class can
// access the private members of this class.
friend class CatArray;
private:
static int ID;
public:
void dog(const char* value) {
// Use ID here any way you want.
}
};
int Cat::ID = 0;
class CatArray {
private:
std::vector<Cat> cats;
public:
explicit CatArray(unsigned int size) : cats(size) {}
Cat& operator [](unsigned int index) {
Cat::ID = index;
return cats[index];
}
};
But a little different. There are 18 Clients in a game and i need to basically do this. for eg, "Client 4 Chooses an option and the option gets called through the array index and than that way client 4 will call the function with the function holding the index 4"
Then cats[1] is not really a Cat object but a CatWithIndex object:
class Cat {
public:
void dog(size_t index,const char* value);
};
class CatWithIndex {
size_t index_;
const Cat &cat_;
public:
CatWithIndex(size_t index, const Cat &cat): index_(index), cat_(cat) {}
void dog(const char* value) {
cat_.dog(index_,value);
}
};
class CatArray {
private:
std::vector<Cat> cats;
public:
Cat& operator [](unsigned int index) {
Cat::ID = index;
return CatWithIndex(index,cats[index]);
}
};