I have a small problem with inheritance in my code - it suddenly stops working when I add identical code in another "if" statement in my loop.
Here is the code I use for "main.cpp":
#include "bibl.h"
using namespace std;
int main()
{
int o;
Vec2 test;
while(1)
{
cout<<"1. Add item and show."<<endl<<"2. Show."<<endl;
cin>>o;
if(o==1)
{
InhItem a("Test",100);
test.addItem(&a);
cout<<"This show works:"<<endl;
test.getVec1(0)->getItem(0)->Show();//This code works.
}
else if(o==2)
{
cout<<"This show doesn't work:"<<endl;
test.getVec1(0)->getItem(0)->Show();//This doesn't.
}
}
system("pause");
return 0;
}
And the code for "bibl.h":
#ifndef TEST1
#define TEST1
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Item
{
protected:
string im;
string theme;
public:
Item(string im=" ", string theme=" "):im(im),theme(theme)
{
}
~Item()
{
}
string getTheme()
{
return theme;
}
virtual void Show()
{
}
};
class InhItem:public Item
{
int tst;
public:
InhItem(string im=" ", int tst=0):Item(im),tst(tst)
{
}
~InhItem()
{
}
void Show()
{
cout<<tst<<endl;
}
};
class Vec1
{
vector<Item*> Vec1a;
string theme;
public:
Vec1(string theme=" "):theme(theme)
{
}
~Vec1()
{
}
void addToVec1a(Item *item)
{
Vec1a.push_back(item);
}
string getTheme()
{
return theme;
}
Item *getItem(int p)
{
return Vec1a[p];
}
};
class Vec2
{
vector<Vec1*> Vec2a;
public:
Vec2()
{
}
~Vec2()
{
}
void addToVec2(Vec1 *vec1)
{
Vec2a.push_back(vec1);
}
void addItem(Item *item)
{
for(int i=0;i<=Vec2a.size();i++)
{
if(i==Vec2a.size())
{
addToVec2(new Vec1(item->getTheme()));
Vec2a[i]->addToVec1a(item);
break;
}
else if(Vec2a[i]->getTheme().compare(item->getTheme())==0)
{
Vec2a[i]->addToVec1a(item);
break;
}
}
}
Vec1 *getVec1(int r)
{
return Vec2a[r];
}
};
#endif
When I try to use the 2nd "if" after adding the item with 1st, it doesn't show - in 1st "if" test.getVec1(0)->getItem(0)->Show(); works, but in another it doesn't.
What is the cause of this problem and how can I fix it?
Related
I am a C ++ beginner and I am working with classes and modules. When I try to run the app, the app gives me the following error: zsh: bus error /Users/****/Documents/jacoProject/App. On the internet I searched a lot, but I found things that I didn't understand and that I couldn't apply to my code.
Should I implement a destructor? If so how can I implement it with my class? Thanks in advance
func.cxx
module;
#include <string>
export module airline_ticket;
export class AirlineTicket
{
public:
AirlineTicket();
double calculatePriceInDollars();
std::string getPassengerName();
void setPassengerName(std::string name);
int getNumberOfMiles();
void setNumberOfMiles(int miles);
bool hasEliteSuperRewardsStatus();
void setHasEliteSuperRewardsStatus(bool status);
private:
std::string q_passengerName;
int q_numberOfMiles;
bool q_hasEliteSuperRewardsStatus;
};
func_impl.cxx
module;
#include <string>
module airline_ticket;
AirlineTicket::AirlineTicket()
{
q_passengerName = "..not";
q_numberOfMiles = 0;
q_hasEliteSuperRewardsStatus = false;
}
double AirlineTicket::calculatePriceInDollars()
{
if (hasEliteSuperRewardsStatus())
{
return 0;
}
else
{
return getNumberOfMiles() * 0.1;
}
}
std::string AirlineTicket::getPassengerName()
{
return q_passengerName;
}
void AirlineTicket::setPassengerName(std::string name)
{
q_passengerName = name;
}
int AirlineTicket::getNumberOfMiles()
{
return q_numberOfMiles;
}
void AirlineTicket::setNumberOfMiles(int miles)
{
q_numberOfMiles = miles;
}
bool AirlineTicket::hasEliteSuperRewardsStatus()
{
return q_hasEliteSuperRewardsStatus;
}
void AirlineTicket::setHasEliteSuperRewardsStatus(bool status)
{
q_hasEliteSuperRewardsStatus = status;
}
main.cpp
#include <iostream>
import airline_ticket;
int main()
{
AirlineTicket myTicket;
myTicket.setPassengerName("Will SMith");
myTicket.setNumberOfMiles(450);
double ticketCost {myTicket.calculatePriceInDollars()};
std::cout << ticketCost << std::endl;
return 0;
}
I would like to write a class that stores recursively the same class in a map.
I have the following source codes:
/* A.hpp */
#include <iostream>
#include <string>
#include <map>
class A
{
private:
int a;
std::map<int, A> data;
bool finishCycle;
public:
A(const int& input ) : a(input), finishCycle(false)
{
func1();
}
void func1();
};
/* A.cpp */
void A::func1()
{
A tmp(*this);
while(!finishCycle)
{
a--;
if (a == 0)
finishCycle=true;
else
func1();
}
data.emplace(tmp.a, tmp);
}
/* main.cpp*/
#include "A.hpp"
int main(int argCount, char *args[])
{
A myObj1 (3);
std::cout << "tst" << std::endl;
return 0;
}
This is putting all the entries in the main map. I would like to have it in a nested sequence:
first entry: <1, A>
inside first entry: <2,A>
inside inside first entry <3,A>
How can I change the script to do this? The nested loops still make me confused.
Maybe this is what you want?
#include <iostream>
#include <map>
class A
{
private:
std::map<int, A> data;
public:
A(int n, int i = 0) {
if (n == i) {
return;
}
data.emplace( i+1, A(n, i + 1) );
}
void test() {
if (!data.empty()) {
std::cout << data.begin()->first << "\n";
data.begin()->second.test();
}
}
};
int main()
{
A a(4);
a.test();
}
I came up with:
#include <iostream>
#include <map>
class A
{
private:
int a;
std::map<int, A> data;
bool finishCycle;
public:
A(const int& input ):a(input), finishCycle(false)
{
func1(*this);
}
void func1(A& tmp);
};
void A::func1(A& tmp)
{
A tmp2(tmp);
tmp2.a--;
while(!finishCycle)
{
if (tmp2.a == 0)
{
(*this).finishCycle=true;
}
else
{
func1(tmp2);
}
}
tmp.data.emplace(tmp2.a, tmp2);
}
int main(int argCount, char *args[])
{
A myObj1 (4);
std::cout << "tst" << std::endl;
return 0;
}
I have the following code
Vehicle.h:
#pragma once
class Vehicle
{
public:
Vehicle();
~Vehicle();
private:
int wheels;
};
Car.h
#pragma once
#include "Vehicle.h"
class Car: public Vehicle
{
public:
Car();
~Car();
private:
int wheels=4;
};
ParkingLot.h
#pragma once
#include <vector>
#include <string>
#include "ParkingSpace.h"
#include "HandicappedParkingSpace.h"
#include "CompactParkingSpace.h"
#include "RegularParkingSpace.h"
class ParkingLot
{
public:
ParkingLot();
~ParkingLot();
void ParkVehicle(Vehicle _v, ParkingSpace& _ps, std::string ps);
void ReleaseVehicle(Vehicle _v, ParkingSpace& _ps, std::string ps);
void getOccupiedSpaces();
private:
int value;
std::vector <HandicappedParkingSpace> occupied_handicapparkingspaces;
std::vector <HandicappedParkingSpace> vacant_handicapparkingspaces;
std::vector <RegularParkingSpace> occupied_regularparkingspaces;
std::vector <RegularParkingSpace> vacant_regularparkingspaces;
std::vector <CompactParkingSpace> occupied_compactparkingspaces;
std::vector <CompactParkingSpace> vacant_compactparkingspaces;
};
ParkingLot.cpp:
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include "ParkingLot.h"
using namespace std
ParkingLot::ParkingLot() {
for (int i=0; i<5; i++) {
HandicappedParkingSpace HPS(1, Null);
vacant_handicapparkingspaces.push_back(HPS);
}
for (int i=0; i<5; i++) {
CompactParkingSpace CPS(1, Null);
vacant_compactparkingspaces.push_back(CPS);
}
for (int i=0; i<5; i++) {
RegularParkingSpace RPS(1, Null);
vacant_regularparkingspaces.push_back(RPS);
}
cout<<"finished parking lot"<<endl;
}
void ParkingLot::ParkVehicle(Vehicle _v, ParkingSpace& _ps, std::string ps)
{
if (ps=="Handicapped") {
if (vacant_handicapparkingspaces.size()!=0) {
_ps.parkvehicle(_v);
vacant_handicapparkingspaces.pop_back();
occupied_handicapparkingspaces.push_back(_ps);
}
else
{
cout<<"No handicapped spaces available"<<endl;
}
}
else if (ps=="Compact") {
if (vacant_compactparkingspaces.size()!=0) {
_ps.parkvehicle(_v);
vacant_compactparkingspaces.pop_back();
occupied_compactparkingspaces.push_back(_ps);
}
else
{
cout<<"No compact spaces available"<<endl;
}
}
else {
if (vacant_regularparkingspaces.size()!=0) {
_ps.parkvehicle(_v);
vacant_regularparkingspaces.pop_back();
occupied_regularparkingspaces.push_back(_ps);
}
else {
cout<<"No regular spaces available"<<endl;
}
}
}
void ParkingLot::ReleaseVehicle(Vehicle _v, ParkingSpace& _ps, std::string ps)
{
_ps.vacant=1;
_ps.vehicle= Null;
if (ps=="Handicapped") {
if (occupied_regularparkingspaces.size()!=0) {
vacant_handicapparkingspaces.push_back(_ps);
occupied_handicapparkingspaces.pop_back();
}
else {
cout<<"Unable to release any handicapped spaces"<<endl;
}
}
else if (ps=="Compact") {
if (occupied_regularparkingspaces.size()!=0) {
_ps.parkvehicle(_v);
vacant_compactparkingspaces.pop_back(_ps);
occupied_compactparkingspaces.push_back();
}
else {
cout<<"Unable to release any compact spaces"<<endl;
}
}
else {
if (occupied_regularparkingspaces.size()!=0) {
_ps.parkvehicle(_v);
vacant_regularparkingspaces.pop_back(_ps);
occupied_regularparkingspaces.push_back();
}
else {
cout<<"Unable to release any regular spaces"<<endl;
}
}
}
void ParkingLot::getOccupiedSpaces() {
cout<<"Occupied handicap spaces: "<<occupied_handicapparkingspaces.size()<<endl;
cout<<"Vacant handicap spaces: "<<vacant_handicapparkingspaces.size()<<endl;
cout<<"Occupied compact spaces: "<<occupied_compactparkingspaces.size()<<endl;
cout<<"Vacant compact spaces: "<<vacant_compactparkingspaces.size()<<endl;
cout<<"Occupied regular spaces: "<<occupied_regularparkingspaces.size()<<endl;
}
ParkingSpace.h:
#pragma once
#include "Vehicle.h"
class ParkingSpace
{
public:
ParkingSpace();
~ParkingSpace();
virtual void parkvehicle()=0;
private:
Vehicle *vehicle;
bool vacant;
};
HandicappedParkingSpace.h:
#pragma once
#include "ParkingSpace.h"
class HandicappedParkingSpace : public ParkingSpace
{
public:
HandicappedParkingSpace(int _vacant, Vehicle* _v) {
this->vacant=_vacant;
this->vehicle=_v;
}
~HandicappedParkingSpace();
void parkvehicle(Vehicle _v) {
this->vacant=0;
this->vehicle=_v;
}
private:
int vacant;
Vehicle *vehicle;
};
RegularParkingSpace.h:
#pragma once
#include "ParkingSpace.h"
class RegularParkingSpace : public ParkingSpace
{
public:
RegularParkingSpace(int _vacant, Vehicle* _v) {
this->vacant=_vacant;
this->vehicle=_v;
}
~RegularParkingSpace();
void parkvehicle(Vehicle _v) {
this->vacant=0;
this->vehicle=_v;
}
private:
int vacant;
Vehicle *vehicle;
};
CompactParkingSpace.h:
#pragma once
#include "ParkingSpace.h"
class CompactParkingSpace : public ParkingSpace
{
public:
CompactParkingSpace(int _vacant, Vehicle* _v) {
this->vacant=_vacant;
this->vehicle=_v;
}
~CompactParkingSpace();
void parkvehicle(Vehicle _v) {
this->vacant=0;
this->vehicle=_v;
}
private:
int vacant;
Vehicle *vehicle;
};
main.cpp:
#include "ParkingLot.h"
#include "HandicappedParkingSpace.h"
#include "RegularParkingSpace.h"
#include "CompactParkingSpace.h"
#include "Car.h"
#include <iostream>
using namespace std;
int main()
{
ParkingLot PL;
Car c1;
HandicappedParkingSpace HPS;
PL.ParkVehicle(c1, HPS, "Handicapped");
Car c2;
CompactParkingSpace CPS;
PL.ParkVehicle(c2, CPS, "Handicapped");
PL.getOccupiedSpaces();
cout<<"FINISHED"<<endl;
//delete d;
return 0;
}
This is the error I get: https://pastebin.com/p0vzb0Mz (the error was so long I wasn't able to post it here)
Can anyone help with this?
EDIT:
I modified ParkingLot.cpp so it now looks like
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include "ParkingLot.h"
using namespace std;
ParkingLot::ParkingLot() {
for (int i=0; i<5; i++) {
HandicappedParkingSpace HPS(1, nullptr);
vacant_handicapparkingspaces.push_back(HPS);
}
for (int i=0; i<5; i++) {
CompactParkingSpace CPS(1, nullptr);
vacant_compactparkingspaces.push_back(CPS);
}
for (int i=0; i<5; i++) {
RegularParkingSpace RPS(1, nullptr);
vacant_regularparkingspaces.push_back(RPS);
}
cout<<"finished parking lot"<<endl;
}
void ParkingLot::ParkVehicle(Vehicle _v, ParkingSpace& _ps, std::string ps)
{
if (ps=="Handicapped") {
if (vacant_handicapparkingspaces.size()!=0) {
_ps.parkvehicle(_v);
vacant_handicapparkingspaces.pop_back();
occupied_handicapparkingspaces.push_back(_ps);
}
else
{
cout<<"No handicapped spaces available"<<endl;
}
}
else if (ps=="Compact") {
if (vacant_compactparkingspaces.size()!=0) {
_ps.parkvehicle(_v);
vacant_compactparkingspaces.pop_back();
occupied_compactparkingspaces.push_back(_ps);
}
else
{
cout<<"No compact spaces available"<<endl;
}
}
else {
if (vacant_regularparkingspaces.size()!=0) {
_ps.parkvehicle(_v);
vacant_regularparkingspaces.pop_back();
occupied_regularparkingspaces.push_back(_ps);
}
else {
cout<<"No regular spaces available"<<endl;
}
}
}
void ParkingLot::ReleaseVehicle(Vehicle _v, ParkingSpace& _ps, std::string ps)
{
//_ps.vacant=1;
//_ps.vehicle= nullptr;
_ps.setVehicle(1, nullptr);
if (ps=="Handicapped") {
if (occupied_regularparkingspaces.size()!=0) {
vacant_handicapparkingspaces.push_back(_ps);
occupied_handicapparkingspaces.pop_back();
}
else {
cout<<"Unable to release any handicapped spaces"<<endl;
}
}
else if (ps=="Compact") {
if (occupied_regularparkingspaces.size()!=0) {
_ps.parkvehicle(_v);
vacant_compactparkingspaces.push_back(_ps);
occupied_compactparkingspaces.pop_back();
}
else {
cout<<"Unable to release any compact spaces"<<endl;
}
}
else {
if (occupied_regularparkingspaces.size()!=0) {
_ps.parkvehicle(_v);
vacant_regularparkingspaces.push_back(_ps);
occupied_regularparkingspaces.pop_back();
}
else {
cout<<"Unable to release any regular spaces"<<endl;
}
}
}
void ParkingLot::getOccupiedSpaces() {
cout<<"Occupied handicap spaces: "<<occupied_handicapparkingspaces.size()<<endl;
cout<<"Vacant handicap spaces: "<<vacant_handicapparkingspaces.size()<<endl;
cout<<"Occupied compact spaces: "<<occupied_compactparkingspaces.size()<<endl;
cout<<"Vacant compact spaces: "<<vacant_compactparkingspaces.size()<<endl;
cout<<"Occupied regular spaces: "<<occupied_regularparkingspaces.size()<<endl;
cout<<"Vacant regular spaces: "<<vacant_regularparkingspaces.size()<<endl;
}
ParkingSpace.h is now
#pragma once
#include "Vehicle.h"
class ParkingSpace
{
public:
ParkingSpace();
~ParkingSpace();
virtual void parkvehicle(Vehicle _v)=0;
virtual void setVehicle(bool vacant, Vehicle _v);
private:
Vehicle vehicle;
bool vacant;
};
HandicappedParkingSpace.h is now
#pragma once
#include "ParkingSpace.h"
class HandicappedParkingSpace : public ParkingSpace
{
public:
HandicappedParkingSpace(int _vacant, Vehicle& _v) {
this->vacant=_vacant;
this->vehicle=_v;
}
~HandicappedParkingSpace();
void parkvehicle(Vehicle _v) {
this->vacant=0;
this->vehicle=_v;
}
void setVehicle(bool _vacant, Vehicle _v) {
this->vacant=_vacant;
this->vehicle= _v;
}
private:
int vacant;
Vehicle vehicle;
};
RegularParkingSpace.h is now
#pragma once
#include "ParkingSpace.h"
class RegularParkingSpace : public ParkingSpace
{
public:
RegularParkingSpace(int _vacant, Vehicle& _v) {
this->vacant=_vacant;
this->vehicle=_v;
}
~RegularParkingSpace();
void parkvehicle(Vehicle _v) {
this->vacant=0;
this->vehicle=_v;
}
void setVehicle(bool _vacant, Vehicle _v) {
this->vacant=_vacant;
this->vehicle= _v;
}
private:
int vacant;
Vehicle vehicle;
};
CompactParkingSpace.h is now
#pragma once
#include "ParkingSpace.h"
class CompactParkingSpace : public ParkingSpace
{
public:
CompactParkingSpace(int _vacant, Vehicle& _v) {
this->vacant=_vacant;
this->vehicle=_v;
}
~CompactParkingSpace();
void parkvehicle(Vehicle _v) {
this->vacant=0;
this->vehicle=_v;
}
void setVehicle(bool _vacant, Vehicle _v) {
this->vacant=_vacant;
this->vehicle= _v;
}
private:
int vacant;
Vehicle vehicle;
};
main.cpp is now
#include "ParkingLot.h"
#include "HandicappedParkingSpace.h"
#include "RegularParkingSpace.h"
#include "CompactParkingSpace.h"
#include "Car.h"
#include <iostream>
using namespace std;
int main()
{
ParkingLot PL;
Car c1;
HandicappedParkingSpace HPS(1, nullptr);
PL.ParkVehicle(c1, HPS, "Handicapped");
Car c2;
CompactParkingSpace CPS(1, nullptr);
PL.ParkVehicle(c2, CPS, "Handicapped");
PL.getOccupiedSpaces();
cout<<"FINISHED"<<endl;
//delete d;
return 0;
}
But now I get these errors, it seems they're mostly with the nullptr: https://pastebin.com/hVdcSc63
I thought the push_back errors were because I needed to change the std:vectors such as occupied_handicapparkingspaces from <HandicappedParkingSpace> to <HandicappedParkingSpace&>, then I get this error: https://pastebin.com/QkWC6SRk
cannot declare variable of abstract type for Parking Lot OOP in C++
ParkingSpace is an abstract class because of virtual void parkvehicle()=0;, HandicappedParkingSpace inherits ParkingSpace without defining parkvehicle so it is also an abstract class, then you cannot instantiate HandicappedParkingSpace and this why you have the error about HandicappedParkingSpace HPS(1, Null); and equivalent cases
Note also Null must be replaced by NULL or better nullptr, and you have plenty other errors, I just answer about your title ...
After your edit
I encourage you to not mix using namespace std; and std::xxx, remove all your using if you use the std prefix
ParkingLot.cpp:13:41: error: no matching function for call to ‘HandicappedParkingSpace::HandicappedParkingSpace(int, std::nullptr_t)’
HandicappedParkingSpace HPS(1, nullptr);
The type of the second parameter of the constructor of HandicappedParkingSpace is Vehicle&, nulltr is not compatible with, you confuse Vehicle& and Vehicle*, only a pointer can values nulltr
I thought the push_back errors were because I needed to change the std:vectors such as occupied_handicapparkingspaces from <HandicappedParkingSpace> to <HandicappedParkingSpace&>
no, the problem is you do occupied_handicapparkingspaces.push_back(_ps); while ps_ is a std::string, a std::string is not a HandicappedParkingSpace and there is no conversion from a std::string to a HandicappedParkingSpace
I have written a code like bill payment. Code is working fine but there are many warnings in my code which I want to remove. One of the most frequent warning is deprecated conversion from string constant to 'char* . I have tried many things and some of the warnings are gone but not all. Please anybody point out at my mistakes??
P.S: I have already tried replacing char* to const char* , but then I am not able to exchange or swap values and it was causing error. Any other solution??
Below is the code
#include<iostream>
#include<string.h>
using namespace std;
class item
{
private:
int barcode;
char* item_name;
public:
item (int num=0, char* name="NULL") : barcode(num)
{
item_name=new char[strlen(name)+1];
strcpy(item_name,name);
}
void setbarcode(int num)
{
barcode=num;
}
int getbarcode()
{
return barcode;
}
void scanner()
{
int num;
cin>>num;
setbarcode(num);
}
void printer()
{
cout <<"\nBarcode"<<"\t\t"<<"Item Name"<<"\t\t"<<"Price"<<endl;
cout <<barcode<<"\t\t"<<item_name<<"\t\t\t";
}
~item()
{
delete[]item_name;
}
};
class packedfood : public item
{
private :
int price_per_piece;
public :
packedfood(int a=0, int num=0, char* name = "NULL") : price_per_piece(a),item(num,name)
{
}
void setprice(int num)
{
price_per_piece=num;
}
int getprice()
{
return price_per_piece;
}
void scanner()
{
item::scanner();
}
void printer()
{
item::printer();
cout<<getprice()<<" Per Piece";
}
~packedfood()
{
}
};
class freshfood: public item
{
private:
int price_per_rupee;
float weight;
public:
freshfood(float b=0, int a=0,int num=0,char* name="NULL") : weight(b),price_per_rupee(a),item(num,name)
{
}
void setweight(float b)
{
weight=b;
}
int getweight()
{
return weight*50;
}
void printer()
{
item::printer();
cout<<getweight()<<" Per Rupee"<<endl<<endl;
}
void scanner()
{
item::scanner();
}
~freshfood()
{
}
};
int main()
{
item x(389,"Anything");
item y;
packedfood m(10,118,"Chocolate");
packedfood n;
freshfood r(20.9,93,357,"Fruits");
freshfood s;
cout <<"\n\n Enter the Barcode for Packed food : ";
m.scanner();
m.printer();
cout <<"\n\n Enter the Barcode for Fresh food : ";
r.scanner();
r.printer();
system("PAUSE");
return 0;
}
Same code that I was working on last night, has thrown up a whole new error. One that I've never encountered before, and I am at the point of considering throwing things at my PC. But, everyone here was very helpful last night, so I thought I'd see if anyone had any ideas on this new problem.
Something is causing "Microsoft C++ exception: std::bad_alloc" and I think it's within the first line of the main.cpp, but as that is just creating the player as a Hero (child of creature) class. I can't see why it can't do it.
I know it's probably something stupid, that I've done badly ... but any help would be appreciated!
//main.cpp
#include "Creature.h"
#include "Hero.h"
#include "Monster.h"
#include <conio.h>
using namespace std;
int main()
{
Hero player(1);
Monster baddie;
player.setX(1);
player.setY(1);
baddie.setX(20);
baddie.setY(20);
player.Display();
baddie.Display();
baddie.chase(player);
player.Display();
baddie.Display();
_getch();
return 0;
}
===================================
//Creature.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Creature
{
protected:
int m_xpos;
int m_ypos;
string m_name;
public:
Creature(string name, int xpos, int ypos);
void Display(void);
void left(void);
void right(void);
void up (void);
void down(void);
void setX(int x);
void setY(int y);
int getX(void);
int getY(void);
};
===================================
//monster.h
#pragma once
#include "Creature.h"
class Monster : public Creature
{
public:
Monster();
void chase(class Hero);
bool eaten(class Hero);
};
===================================
//Hero.h
#pragma once
#include "Creature.h"
class Hero : public Creature
{
private:
int m_lives;
int m_score;
public:
Hero(int lives);
void Display(void);
void setScore(void);
};
===================================
//creature.cpp
#include "Creature.h"
Creature::Creature(string name, int xpos, int ypos)
{
m_xpos = xpos;
m_ypos = ypos;
m_name = name;
}
void Creature::Display(void)
{
cout << m_name << endl;
cout << m_xpos << endl;
cout << m_ypos << endl;
}
void Creature::left(void)
{
m_xpos = m_xpos+1;
}
void Creature::right(void)
{
m_xpos = m_xpos-1;
}
void Creature::up(void)
{
m_ypos = m_ypos-1;
}
void Creature::down(void)
{
m_ypos = m_ypos+1;
}
void Creature::setX(int x)
{
m_xpos = x;
}
void Creature::setY(int y)
{
m_ypos = y;
}
int Creature::getX(void)
{
return m_xpos;
}
int Creature::getY(void)
{
return m_ypos;
}
===================================
//Hero.cpp
#include "Creature.h"
#include "Hero.h"
Hero::Hero(int lives) : Creature(m_name, m_xpos, m_ypos)
{
m_lives = lives;
}
void Hero::Display(void)
{
Creature::Display();
cout << "Lives: " << m_lives << endl;
cout << "Score: " << m_score << endl;
}
void Hero::setScore(void)
{
m_score = 0;
}
===================================
//Monster.cpp
#include "Creature.h"
#include "Monster.h"
#include "Hero.h"
Monster::Monster() : Creature(m_name, m_xpos, m_ypos)
{
}
void Monster::chase(Hero hero)
{
if(getX() < hero.getX())
{
right();
}
if(getX() > hero.getX())
{
left();
}
if(getX() < hero.getX())
{
down();
}
if(getX() >hero.getX())
{
up();
}
}
bool Monster::eaten(Hero hero)
{
if((getX() == hero.getX())&&(getX() == hero.getX()))
{
return true;
}
}
===================================
The problem lay in Hero::Hero(int lives) : Creature(m_name, m_xpos, m_ypos) and the equivilant with the Monster.cpp file.
Changing them to Hero::Hero(int lives) : Creature("", 0,0) fixed the memory problem.
Thanks again to a wonderful community!
Hopefully, you'll never see this code again! (fingers crossed!)
The error is with this line:
Hero::Hero(int lives) : Creature(m_name, m_xpos, m_ypos)
You cannot create the Creature sub-object by passing its own uninitialized data members to it. You need to pass some sort of valid values to the base-class constructor, like Creature("", 0, 0) for example.
The error is caused, somehow, by the attempt to copy an uninitialized std::string object.