This question already has answers here:
How do I declare a 2d array in C++ using new?
(29 answers)
Closed 2 years ago.
I am new to C++ and would like to access the values in a subclass.
When I am trying to access the values, my program is crashing and returning stack-dump.
For example:
class test{
protected:
std::string name;
int points;
object** inventory;
public:
test(const std::string name, int points) : name(name), points(points), inventory(new object*[10]()) {
for(int i = 0; i < 10; i++) {
this->inventory[i]->setValid(false);
}
}
class object {
protected:
bool isValid;
std::string name;
int value;
public:
object(const std::string name, int value) : name(name), value(value), isValid(false) {}
const std::string getName();
bool getValid();
void setValid(bool isValid);
};
In the header file.:
void object::setValid(bool isValid) {
this->isValid = isValid;
//std::cout << isValid; returning of isValid is possible, but not of this->isValid
}
The necessary header files and declarations are included.
While debugging it stops while trying to get the value of this->isValid in my class object with the following error message:
Failed to execute MI command:
-data-evaluate-expression ((this)->isValid)
Error message from debugger back end:
Cannot access memory at address 0xc
Do I use an incorrect pointer? How can I solve the issue?
This is a pointer to a pointer to an object. You allocated an array of pointers to objects, but not the objects themselves.
object** inventory;
Once you do this:
inventory(new object*[10]())
You can now access inventory[0] through inventory[9]. But they aren't set to anything yet. They may not even be null, it's just garbage memory.
You can allocate the objects in the loop:
for(int i = 0; i < 10; i++) {
inventory[i] = new object();
inventory[i]->setValid(false);
}
But, you'll need to remember to free all of those objects. You might consider using array allocators to allocate an array of objects instead of an array of pointers to objects. But since this is C++, better to use a vector.
std::vector<object> inventory
Related
I'm confused as to what I'm doing wrong? While debugging, this shows 0xcdcdcdcd {theDouble=??? }so i know my variable isnt getting stored in my mutator. How would i go about fixing this issue? Am I supposed initialize this somewhere? I am using visual studio by the way.
Thank you for the help.
int main()
{
int i = 0;
const int MAX = 4;
CLASS **object = new CLASS*[MAX];
string name = "todd";
string aString = "words";
double aDouble = 10.0;
object[i]->setDouble(aDouble);
return 0;
}
//.cpp
CLASS::Class()
{
theDouble = new double;
*theDouble = NULL;
}
CLASS::Class(double aDouble)
{
*theDouble = aDouble;
}
void CLASS::setDouble(double p)
{
*theDouble = p;
double Class::getDouble()
{return (*theDouble);}
//.h
class CLASS
{
protected:
double *theDouble;
public:
Insurance();
Insurance(double premium);
//~Insurance();
void setDouble(double p);
double getDouble();
string toString();
};`
You use CLASS, Class and Insurance in mix where you should use one name so your posted code can not compile. I replace those with IronMan.
What you see as 0xCDCDCDCD is not this pointer value but value of the pointer member theDouble. The issue comes from in constructor:
IronMan::IronMan(double aDouble)
{
*theDouble = aDouble;
}
That constructor dereferences uninitialised member variable theDouble and
since debuggers tend to fill uninitialized memory with some bytes like 0xCD you will have that as value of the pointer. Therefore you get a crash for accessing memory at such address.
One way out of it is to allocate memory for theDouble
IronMan::IronMan(double aDouble)
{
theDouble = new double(aDouble);
}
Better would be likely to avoid using pointers there at all but just have direct double data member:
protected:
double theDouble;
I have 1 question because I am pretty curious how to handle with such problem.
I have base class called "Pracownik" (Worker) and 2 subclasses which are made from public Pracownik;
- Informatyk (Informatic)
- Księgowy (Accountant)
Writing classes is easy. Made them pretty fast but I have small problem with main because I am helping friend with program but I was not using C++ for a while. So:
This is my header file "funkcje.h"
#include <iostream>
using namespace std;
class Pracownik
{
private:
string nazwisko;
int pensja;
public:
Pracownik(string="",int=0);
~Pracownik();
string getNazwisko();
int getPensja();
friend double srednia_pensja(int,Pracownik);
};
class Informatyk : public Pracownik
{
private:
string certyfikat_Cisco;
string certyfikat_Microsoft;
public:
Informatyk(string="",int=0, string="", string="");
~Informatyk();
void info();
};
class Ksiegowy : public Pracownik
{
private:
bool audytor;
public:
Ksiegowy(string="",int=0, bool=false);
~Ksiegowy();
void info();
};
double srednia_pensja(int,Pracownik);
These are definitions of my functions "funkcje.cpp"
#include "funkcje.h"
Pracownik::Pracownik(string a,int b)
{
nazwisko=a;
pensja=b;
}
Pracownik::~Pracownik()
{
}
string Pracownik::getNazwisko()
{
return nazwisko;
}
int Pracownik::getPensja()
{
return pensja;
}
Informatyk::Informatyk(string a, int b, string c, string d) : Pracownik(a,b)
{
certyfikat_Cisco=c;
certyfikat_Microsoft=d;
}
Informatyk::~Informatyk()
{
}
Ksiegowy::Ksiegowy(string a, int b, bool c) : Pracownik(a,b)
{
audytor=c;
}
Ksiegowy::~Ksiegowy()
{
}
void Informatyk::info()
{
cout<<"Nazwisko pracownika: "<<Pracownik::getNazwisko()<<endl;
cout<<"Pensja pracownika: "<<Pracownik::getPensja()<<endl;
cout<<"Certyfikat Cisco: "<<certyfikat_Cisco<<endl;
cout<<"Certyfikat Microsoft: "<<certyfikat_Microsoft<<endl;
}
void Ksiegowy::info()
{
cout<<"Nazwisko pracownika: "<<Pracownik::getNazwisko()<<endl;
cout<<"Pensja pracownika: "<<Pracownik::getPensja()<<endl;
cout<<"Audytor: ";
if(audytor)
cout<<"Tak"<<endl;
else
cout<<"Nie"<<endl;
}
double srednia_pensja(int a,Pracownik *b)
{
return 0;
}
And finally main!
#include <iostream>
#include "funkcje.h"
using namespace std;
int main()
{
Pracownik lista[10];
Pracownik *lista_wsk = new Pracownik[10];
Informatyk a("Kowalski1",1000,"Cisco1","Microsoft1");
Informatyk b("Kowalski2",2000,"Cisco2","Microsoft2");
Informatyk c("Kowalski3",3000,"Cisco3","Microsoft3");
Ksiegowy d("Kowalski4",4000,1);
Ksiegowy e("Kowalski5",5000,0);
lista[0]=a;
lista[1]=b;
lista[2]=c;
lista[3]=d;
lista[4]=e;
Informatyk *ab = new Informatyk("Kowalski1",1000,"Cisco1","Microsoft1");
Informatyk *ac = new Informatyk("Kowalski2",2000,"Cisco2","Microsoft2");
Informatyk *ad = new Informatyk("Kowalski3",3000,"Cisco3","Microsoft3");
Ksiegowy *ae = new Ksiegowy("Kowalski4",3000,1);
Ksiegowy *af = new Ksiegowy("Kowalski5",3000,0);
lista_wsk[0]=*ab;
lista_wsk[1]=*ac;
lista_wsk[2]=*ad;
lista_wsk[3]=*ae;
lista_wsk[4]=*af;
for(int i;i<5;i++)
{
lista[i].info();
cout<<endl;
}
cout<<endl;
// for(int i;i<5;i++)
// {
// lista_wsk[i].info();
// }
return 0;
}
Ok and here goes my questions:
I had to create array which is filled with base class objects "Pracownik".
Secondary i had to create array which is full of pointers to class "Pracownik" objects.
(Hope those 2 first steps are done correctly)
Next thing I had to write to array 3 objects of class Informatic and 2 of class Accountant.
So I ve created 5 objects manually and added them into the array in such way array[0]=a;. I guess this is still good.
Next thing i had to create and add similar objects to array of pointers using new. So I ve created array with new and pointers to objects with new. (Hope thats correct 2).
And FINALLY:
I had to use info() on added to array objects.
This is my main question if my array is type "Pracownik" and I want to use function info() from subclasses how should I do that? And how compiler will know if he should use info() from Accountant or Informatic while I am trying to show those information using "for".
In an array of Pracownik, the elements are of type Pracownik. Any information about the objects being of a subclass of Pracownik are lost when you copy the elements into the array.
This is called object slicing and leads to the fact that there is no way to invoke Informatyk::info() on these objects.
If you want to call methods of a subclass, you have to prevent object slicing by storing pointers or references in the array.
As Oswald says in his answer,
Pracownik * lista_wsk = new Pracownik[10];
allocates an array of 10 Pracownik objects. This is probably not what you want. With polymorphism involved, we usually want to deal with pointers or references. Hence, you'd want an array of Pracownik * pointers. Since you already know at compile-time that it will have 10 members, there is no need for a dynamic allocation here. I think you've meant to write
Pracownik * lista_wsk[10];
instead. Now we don't put objects but pointers to objects into the array. For example:
lista_wsk[2] = new Informatyk("Kowalski3", 3000, "Cisco3", "Microsoft3");
And then we can iterate over the items like so:
for (unsigned i = 0; i < 10; ++i)
std::cout << lista_wsk[i]->getNazwisko() << std::endl;
As you have already discovered, it is impossible to call a subclass function member on a superclass object. It would be possible to figure out the actual type at run-time yourslf by means of a cast.
for (unsigned i = 0; i < 10; ++i)
if (Informatyk * info_ptr = dynamic_cast<Informatyk *>(lista_wsk[i]))
info_ptr->info();
dynamic_cast returns a pointer to the target class if this is possible or a nullptr (which evaluates to false, hence the conditional) otherwise. Note however that this is considered very poor style. It is better to use virtual functions. Therefore, add
virtual void
info()
{
// Do what is appropriate to do for a plain Pracownik.
// Maybe leave this function empty.
}
to the superclass and again to the subclass
virtual void
info() // override
{
// Do what is appropriate to do for an Informatyk.
}
The function in the subclass with the same signature is said to override the function inherited from the superclass. Since the function is marked as virtual, the compiler will generate additional code to figure out at run-time what version of the function to call.
If you are coding C++11, you can make the override explicit by placing the keyword override after its type as shown above (uncomment the override). I recommend you use this to avoid bugs that arise from accidental misspelling or other typos.
The problem occurs in my ToDoList.cpp class file.
ToDoList.cpp:
ToDoList::ToDoList() {
arraySize = 3;
arrayData = 0;
array = new string(arraySize); //error here
}
ToDoList::ToDoList() {
array = new string(todolist.arraySize); //and error here
arraySize = todolist.arraySize;
arrayData = todolist.arraySize;
}
ToDoList.h:
class ToDoList {
public:
ToDoList();
ToDoList(const ToDoList&);
~ToDoList();
void AddItem(string item);
void ListItems();
private:
string* array;
int arraySize;
int arrayData;
};
If you want an array of strings, use
array = new string[arraySize];
You should use
array = new string[arraySize];
and
array = new string[todolist.arraySize];
In your example, you are trying to create an object of std::string class(string(arrraySize)) which is not valid. Compiler is giving the appropriate error to understand it.
EDIT
Your class may be written using std::vector which is efficient correct and easy to understand code.
class ToDoList {
public:
ToDoList() {};
~ToDoList() {};
void AddItem(std::string& item);
void ListItems();
private:
std::vector<std::string> array;
};
void ToDoList::AddItem(std::string& item) {
array.push_back(item);
}
void ToDoList::ListItems() {
for(size_t i = 0; i<array.size(); i++) {
std::cout<<array[i]<<std::endl;
}
}
There's no std::string constructor with just a length. If you want to initialise a string with something in it, you need to say what characters to use. You could use new string(arraySize, fillCharacter), but given this usage, maybe std::vector may be more appropriate.
Edit: the extra details shows you are trying to do something different. So see the other answers. However it looks like you are trying to re-invent a vector<string> , so you may find it easier to use a std::vector instead of manually allocating your array of strings.
I'm trying to make a chess program, but I want to be able to implement different AIs in it. Thus I made a abstract AIgeneric class and the derived class AIrandom off of AIgeneric. Then in my chessAI interface, I create a list of the the AIs, and try to call their getNextMove function and run into a segfault. The code is as below:
class AIgeneric {
public:
virtual int getNextMove(int*, const int &) = 0;
}
class AIrandom : public AIgeneric {
public:
AIrandom();
virtual int getNextMove(int*, const int &);
}
class chessAI {
public:
chessAI();
~chessAI();
void setAI();
int getNextMove(int*, const int &);
private:
vector<AIgeneric*> AIlist;
vector<string> names;
int selectedAI;
};
chessAI::chessAI () {
AIrandom randomAI;
AIlist.push_back(&randomAI);
names.push_back("Random AI");
selectedAI = -1;
}
int chessAI::getNextMove(int * board, const int & color) {
return AIlist[selectedAI]->getNextMove(board, color); //segfault on this line
}
It'd be great if anyone could help me on this problem!
Edit: I do set selectedAI to 0 before calling getNextMove.
In this code:
chessAI::chessAI () {
AIrandom randomAI;
AIlist.push_back(&randomAI);
names.push_back("Random AI");
selectedAI = -1;
}
You store a pointer to a local variable into your vector. After the constructor returns that pointer is no longer valid.
Remember that all local variables are stored on the stack, and the stack is reused in other functions. So when you use the pointer in the vector, it now points to some other functions memory and not the one object you declared.
This can be solved in three ways:
Allocate the object on the heap:
AIlist.push_back(new AIRandom);
Not using pointers at all.
Use smart pointers, such as std::unique_ptr.
You call selectedAI = -1; and then AIlist[selectedAI]->.... What do you expect AIlist[-1] to be, other than undefined behavior?
I expect this is because AIlist[selectedAI] is out of bounds. You can confirm this by replacing it with AIlist.at(selectedAI). Keep in mind that this index is -1 immediately after the constructor...
I have a pretty standard class with some public member functions and private variables.
My problem originally stems from not being able to dynamically name object instances of my class so I created an array of pointers of the class type:
static CShape* shapeDB[dbSize];
I have some prompts to get info for the fields to be passed to the constructor (this seems to work):
shapeDB[CShape::openSlot] = new CShape(iParam1,sParam1,sParam2);
openSlot increments properly so if I were to create another CShape object, it would have the next pointer pointing to it. This next bit of code doesn't work and crashes consistently:
cout << shapeDB[2]->getName() << " has a surface area of: " << shapeDB[2]->getSA() << shapeDB[2]->getUnits() << endl;
The array of pointers is declared globally outside of main and the get() functions are public within the class returning strings or integers. I'm not sure what I'm doing wrong but something relating to the pointer set up I'm sure. I'm writing this code to try and learn more about classes/pointers and have gotten seriously stumped as I can't find anyone else trying to do this.
I'm also curious as to what the CShape new instances get named..? if there is any other way to dynamically create object instances and track the names so as to be able to access them for member functions, I'm all ears.
I've tried all sorts of permutations of pointer referencing/de-referencing but most are unable to compile. I can post larger chunks or all of the code if anyone thinks that will help.
class CShape {
int dim[maxFaces];
int faces;
string units;
string type;
string name;
bool initialized;
int slot;
public:
static int openSlot;
CShape();
CShape(int, string, string); // faces, units, name
~CShape();
void initialize(void);
// external assist functions
int getA(void) {
return 0;
}
int getSA(void) {
int tempSA = 0;
// initialize if not
if(initialized == false) {
initialize();
}
// if initialized, calculate SA
if(initialized == true) {
for(int i = 0; i < faces; i++)
{
tempSA += dim[i];
}
return(tempSA);
}
return 0;
}
string getUnits(void) {
return(units);
}
string getName(void) {
return(name);
}
// friend functions
friend int printDetails(string);
};
// constructor with values
CShape::CShape(int f, string u, string n) {
initialized = false;
faces = f;
units = u;
name = n;
slot = openSlot;
openSlot++;
}
My guess is you use the CShape constructor to increment CShape::openSlot?
You're probably changing the value before it's read, thus the pointer is stored in a different location.
Try replacing openSlot with a fixed value to rule out this CShape::option.
-- code was added --
I'm pretty sure this is the problem, the constructor is executed before the asignment, which means the lhs. will be evaluated after CShape::openSlot is incremented.