I have an abstract class with 2 pure virtual functions:
class CCobjectBase
{
public:
CCobjectBase();
virtual void setCordFigure(double *, int) = 0;
virtual double* getCord() = 0;
};
And a class that is derived from the abstract one:
class CTriangle : public CCobjectBase
{
public:
CTriangle();
~CTriangle();
void setCordFigure(double *, int);
double* getCord();
private:
double *m_cord;
int m_size;
}
void CTriangle :: setCordFigure(double *cord, int size)
{
m_cord = cord;
m_size = size;
}
double * CTriangle :: getCord()
{
return m_cord;
}
I used a vector of pointers to the abstract class:
std::vector<CCobjectBase *> m_objectBaseList;
And i create and store some values in this vector as follow:
m_objectBaseList.push_back(new CTriangle());
m_objectBaseList.at(m_objectBaseList.size() - 1) -> setCordFigure(coordonate, size);
The problem occurs when i try to display the values from the vector:
for(size_t i=0; i< m_objectBaseList.size(); i++)
{
double * cord = m_objectBaseList.at(i) -> getCord();
out<<cord[0]<<" "<<cord[1]<<" "<<cord[2]<<" "<<cord[3]<<" "<<cord[4]<<" "<<cord[5]<<endl;
}
It seems that all the objects have the same values stored. I tried multiple solution but i can not simply figure it out. If i give inputs as follow:
Object1: 1 2 3 4 5 6
Object2: 3 2 3 3 5 5
...
Objectn: 1 1 2 2 1 1
All objects have the value of Objectn:
Object1: 1 1 2 2 1 1
Object2: 1 1 2 2 1 1
...
Objectn: 1 1 2 2 1 1
The problem is not from the parameter coordonate because i tested in the setCordFigure(double *cord, int size){} function and it shows fine.
Any possible sugestions?
EDIT:
The portion of the code where I set the values is actually like this:
void MainWindow :: creazaObiecte(int numarOrdine, double *coordonate, int size)
{
QTextStream out(stdout);
switch(numarOrdine)
{
case 1:
m_objectBaseList.push_back(new CTriangle());
m_objectBaseList.at(m_objectBaseList.size() - 1) -> setCordFigure(coordonate, size);
break;
...
}
}
I use this function from each line of a file what I read. The portion of the code where I call this function is something like:
while(--condition--)
{
double coordonate[30]; //coordonate is used to store values as an array Ex: 20 30 12 2 32 12 etc
//fill coordonate with values from the line of the file
creazaObiecte(ordinClasa, coordonate, size); //ordinClasa is a variable that stores the index of the object (1 stands for triangle)
}
EDIT: My original answer was in a context where I didn't realise m_coord is meant to be an array, so was inappropriate.
Your operation setCordFigure just sets your m_coord member pointer to the passed argument. It does not not copy the underlying array. So in the end all your objects end up pointing to the same array.
You need to actually copy the array in your setCordFigure function. You could do this with naked arrays, but its better to use a vector. So this is possible code:
class CCobjectBase
{
public:
CCobjectBase();
virtual void setCordFigure(const std::vector<double>&) = 0;
virtual std::vector<double>& getCord() = 0;
};
class CTriangle : public CCobjectBase
{
public:
CTriangle();
~CTriangle();
void setCordFigure(const std::vector<double>&);
std::vector<double>& getCord();
private:
std::vector<double> m_cord;
int m_size;
}
void CTriangle :: setCordFigure(const std::vector<double>& cord)
{
m_cord = cord;
}
std::vector<double>& CTriangle :: getCord()
{
return m_cord;
}
Then your creation function becomes:
void MainWindow :: creazaObiecte(int numarOrdine, const std::vector<double>& coordonate)
{
QTextStream out(stdout);
switch(numarOrdine)
{
case 1:
m_objectBaseList.push_back(new CTriangle());
m_objectBaseList.at(m_objectBaseList.size() - 1) -> setCordFigure(coordonate);
break;
...
}
}
And your population function becomes:
while(--condition--)
{
std::vector<double> coordonate(30); //coordonate is used to store values as an array Ex: 20 30 12 2 32 12 etc
//fill coordonate with values from the line of the file
creazaObiecte(ordinClasa, coordonate); //ordinClasa is a variable that stores the index of the object (1 stands for triangle)
}
double *m_cord;
You are storing a pointer to the coordinate in your class.
void CTriangle :: setCordFigure(double *cord, int size)
Then, probably, you are reading the "input" into some variables, which are then referenced in your CTriangle objects. So as the values of the variables are changed, all of the objects are really pointing to the same stuff.
Related
I've been trying for a long time to pass an array of objects to another class object.
In settingUp.cpp:
//** Status classes and their functions **//
void settingUp(){
dataClass prueba0;
dataClass prueba1;
dataClass prueba2;
const dataClass * arrayPrueba[3];
prueba0.setValues(1);
prueba1.setValues(2);
prueba2.setValues(3);
arrayPrueba[0] = &prueba0;
arrayPrueba[1] = &prueba1;
arrayPrueba[2] = &prueba2;
statusClass status;
status.setValues(1, arrayPrueba);
status.printValues();
}
In classData.cpp:
//** dataClass and their functions **//
void dataClass::setValues(int _length){
length = _length;
}
void dataClass::printValues() const{
printf("TP: dataClass: length = %d\n", &length);
};
In statusClass.cpp:
//** Status classes and their functions **//
void statusClass::setValues (uint8_t _statusSelectorByte, const dataClass **_array){
newStatusSelectorByte = _statusSelectorByte;
array = *_array;
};
void statusClass::printValues(){
printf("TP: statusClass -> printValues: Prueba = %d\n", newStatusSelectorByte);
printf("TP: statusClass -> printValues: arrayPrueba = %d\n", array[1].length);
}
When I call:
status.printValues();
I can read only the fist element of the arrayPrueba.
In statusClass::setValues(), *_array is the same as _array[0]. You are storing only the first dataClass* pointer from the input array.
Later, when using array[1], you are mistreating array as-if it were a pointer to an array of objects, when it is really a pointer to a single object instead. You are thus reaching past that object into surrounding memory, which is undefined behavior (but may "work" in this case because an object may happen to actually exist at that location, but this is bad behavior to rely on).
You need to store the original array pointer, not a single element taken from the array.
private:
const dataClass **array; // <-- add an *
void statusClass::setValues (uint8_t _statusSelectorByte, const dataClass **_array){
newStatusSelectorByte = _statusSelectorByte;
array = _array; // <-- get rid of the *
};
void statusClass::printValues(){
printf("TP: statusClass -> printValues: Prueba = %d\n", newStatusSelectorByte);
printf("TP: statusClass -> printValues: arrayPrueba = %d\n", array[1]->length); // use -> instead of .
}
On a side note: in dataClass::printValues(), you need to drop the & when printing the value of length:
printf("TP: dataClass: length = %d\n", length);
I am using push_back to add identical characters and for some reason these characters all have the same stats all the time.
Battle.cpp:
void Battle::InitalizeUser()
{
user.t_Pokemon.clear();
user.t_Item.clear();
user.currentPokemon_U = 0;
user.t_Pokemon.push_back(&uF1);
user.t_Pokemon.push_back(&uF1);
user.t_Pokemon.push_back(&uW3);
user.t_Pokemon.push_back(&uW3);
user.t_Pokemon.push_back(&uG5);
user.t_Pokemon.push_back(&uG5);
user.t_Item.push_back(&uI1);
user.t_Item.push_back(&uI2);
for (unsigned int i = 0; i < user.t_Pokemon.size(); i++)
{
user.t_Pokemon.at(i)->poke_health = 100;
user.t_Pokemon.at(i)->poke_isFainted = false;
}
assert(user.t_Pokemon.size() == 6);
assert(user.t_Item.size() == 2);
}
So if uF1 health is reduced all uF1 characters will suffer the same reduction, but I don't want all the others uF1 to suffer the reduction, I want to be able to distinguish them and have only one specific character affected.
How can I do that?
Pokemon.cpp:
void Pokemon::attackNormal(Pokemon * opponentPokemon)
{
opponentPokemon->poke_health = opponentPokemon->poke_health - 20;
opponentPokemon->changeIfFainted();
assert(opponentPokemon->poke_health <= 100 && opponentPokemon->poke_health >= 0);
}
Battle.h:
class Battle
{
private:
static Trainer user;
static Trainer ash;
static IntRect user_Rect;
static IntRect ash_Rect;
// User's pokemon
static Fire uF1;
static Water uW3;
static Grass uG5;
static Item uI1;
static Item uI2;
}
uF1 : Pokemon.h
class Fire : public Pokemon
{
public:
Fire();
Fire(string name);
virtual ~Fire();
void specialAttack(Pokemon * opponentPokemon);
void changeWeather();
void Draw(RenderWindow &window);
};
The problem is that your vector contains copies of a pointer what is not equivalent to having different copies of an actual object.
It seems that you are passing same pointers several times in order to achieve object copy which is not a case.
You should first declare all your objects and then add their addresses to the vector.
Additional note: you don't have to create a variable for every object you use. You could also create an array of objects.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am working on a chess game in c++ .
I have the classes :
class piece
{
protected :
point position ;
char represent ;
bool colour; // true for player 1 = green , false for player 0 = blue
public :
virtual ~piece() ;
bool find_player(piece* to_check, int num);
char get_char() ;
bool get_colour() ;
virtual bool move_setup(point source, point dest, piece* arr[][SIZE] ) = 0 ;
virtual bool move(point source, point dest, piece* arr[][SIZE]) = 0;
bool same_player(point dest, piece* arr[][SIZE], int* num);
};
class board
{
private:
piece* arr[SIZE][SIZE] ;
public :
board();
void free_borad();
void set_piece(int x , int y , int type , bool colour );
void print_board();
void move(point source , point dest);
void set_colour(int i , int j , bool reset );
};
class pawn : public piece
{
public :
pawn(point position , bool colour );
~pawn();
virtual bool move_setup(point source, point dest, piece* arr[][SIZE]);
virtual bool move(point source, point dest, piece* arr[][SIZE]);
bool if_forward(point source, point dest);
bool if_diagonal(point source, point dest, int* offset);
};
I have a class for every piece in which I have implemented the move functions.
the classes are not relevant so I just put a pawn class for the example .
the board move function :
void board::move(point source, point dest)
{
if (arr[source.get_x()][source.get_y()]) // if not empty
{
int x = source.get_x(), y = source.get_y() ;
if (arr[x][y]->move_setup(source, dest, arr ) ) // if the piece can move there
{
delete arr[dest.get_x()][dest.get_y()];
arr[dest.get_x()][dest.get_y()] = arr[source.get_x()][source.get_y()];
arr[source.get_x()][source.get_y()] = NULL;
std::cout << " Succes! " << std::endl;
}
else
{
// error
}
}
else // empty
{
// error
}
}
my program crashes at the line
if (arr[x][y]->move_setup(source, dest, arr ) )
I have debbuged it using the VS debugger , and realized that the crash occurs when sending the array , error message :
Access violation reading location 0xFFFFFFFF.
I have tried to send it in many different ways , and nothing works ,
but this method is exactlly how a friend of mine did it , and it works just fine for him .
can anyone help ?
thank you .
Arrays in C++ are 0-based, which means the index of the first element is 0. If your coordinates are 1-based, it means you are accessing an element one row and one column off in the array. That in turn leads to breaching the bounds of the array and your crash. The solution is either to adopt a 0-based coordinate system or subtract 1 from the x/y coordinates when accessing the array.
I have a class as below that its method accept an object as its parameter.
template <typename T> class Matrix{
private:
matrixNode<T>* rear=0;
std::vector<int> rows;
std::vector<int> cells;
std::vector<matrixNode<T>*> listofAddedNodes;
void generatetableRowCellArrays(int rowSize,int cellSize){
for (int i=0;i<rowSize;i++){
rows.push_back(i);
}
for (int i=0;i<cellSize;i++){
cells.push_back(i);
}
}
public:
Matrix(int rowSize,int cellSize){
generatetableRowCellArrays(rowSize,cellSize);
}
void ceateSparsMatrix(matrixNode<T> *node){
matrixNode<T> *mxNode=(matrixNode<T>*)malloc(sizeof(matrixNode<T>));
mxNode=node;
if(rear!=0){
rear->PL=(matrixNode<T>*)&rows[mxNode->line];
rear->PC=(matrixNode<T>*)&cells[mxNode->column];
}
rear=mxNode;
listofAddedNodes.push_back(mxNode);
}
void ceateSparsMatrix(matrixNode<T> node,int SpecificRow,int specificCell){
matrixNode<T> *mxNode=(matrixNode<T>*)malloc(sizeof(matrixNode<T>));
mxNode=node;
if(rear!=0){
rear->PL=&rows[SpecificRow];
rear->PC=&cells[specificCell];
}
rear=mxNode;
listofAddedNodesPointers.push_back(&rear->value);
}
void showAllAddedNodes(){
for(int i=0;i<listofAddedNodes.size();i++){
printf("|----------------------------|\n");
printf("|Value | %d |\n",listofAddedNodesPointers[i]->value);
printf("|Coll | %d |\n",listofAddedNodesPointers[i]->column);
printf("|Line | %d |\n",listofAddedNodesPointers[i]->line);
printf("|PC | %d |\n",listofAddedNodesPointers[i]->PC);
printf("|PL | %d |\n",listofAddedNodesPointers[i]->PL);
printf("|----------------------------|\n");
}
}};};
When i want to test this function , i create several objects of the "matrixNode" class and pass to the function but it doesn't work!
Matrix<int> matrix(5,6);
matrixNode<int> *matNode();
matNode->column=2;
matNode->line=0;
matNode->value=2;
matNode->PC=NULL;
matNode->PL=NULL;
matrixNode<int> *matNode2;
matNode2->column=5;
matNode2->line=5;
matNode2->value=5;
matNode2->PC=NULL;
matNode2->PL=NULL;
matrix.ceateSparsMatrix(matNode);
matrix.ceateSparsMatrix(matNode2);
It doesn't pass the objects well!
Can you please help me?
Thanks
You need to either dynamically allocate your matrix objects or pass pointers to existing objects.
Matrix<int> matrix(5,6);
matrixNode<int> * matNode = new matrixNode<int>;
matNode->column=2;
matNode->line=0;
matNode->value=2;
matNode->PC=NULL;
matNode->PL=NULL;
matrixNode<int> * matNode2 = new matrixNode<int>;
matNode2->column=5;
matNode2->line=5;
matNode2->value=5;
matNode2->PC=NULL;
matNode2->PL=NULL;
matrixNode<int> matNode3;
matNode3.column=5;
matNode3.line=3;
matNode3.value=17;
matNode3.PC=NULL;
matNode3.PL=NULL;
matrix.ceateSparsMatrix(matNode);
matrix.ceateSparsMatrix(matNode2);
matrix.ceateSparsMatrix(&matNode3);
This one of your issues.
This is sort of a design doubt .
Scenario: I have an array which contain some integer elements . This array is populated by 1 module (.so) in my code base say X. It is then shared to another module say Y (.so) . At run time X module identifies that module Y would need to work on few fields of the array and modify it and that was the reason X shared the array to Y . ( Both these so are consumed into one binary .)
Once Y returns the module X prints the array .
Problem : How can I enforce programatically that module Y does not modify any other array index other than the one identified by X . SInce the whole array is passed between modules i cant make it const as then Y would not be able to change any field . You can say i want to enforce const-ness for few fields identified at run time .
How about this:
template <class T> class CProtectedArray {
private:
T* m_aItems;
unsigned int* m_aMask;
public:
CProtectedArray(int cElem, bool fInitialProtect) : m_aItems(NULL) {
int cbElem = sizeof(T)*cElem;
int cbMask = sizeof(int)*(cElem+31)/32;
m_aItems = (T*)malloc(cbElem + cbMask);
m_aMask = (unsigned int*)(m_aItems + cElem);
memset(m_aItems, 0, cbElem);
memset(m_aMask, fInitialProtect ? -1 : 0, cbMask);
}
~CProtectedArray() {
if (m_aItems)
free(m_aItems);
}
bool IsProtected(int iItem) { return !!(m_aMask[iItem>>5] & (1<<(iItem&31))); }
void Protect(int iItem) { m_aMask[iItem>>5] |= 1<<(iItem&31); }
void UnProtect(int iItem) { m_aMask[iItem>>5] &= ~(1<<(iItem&31)); }
void Set(int iItem, T val) {
if (!IsProtected(iItem))
m_aItems[iItem] = val;
}
};
int main(int argc, char* argv[])
{
CProtectedArray<int> A(100, true);
bool f = A.IsProtected(30); // f = true
A.Set(30, 23); // nothing happens
A.UnProtect(30);
f = A.IsProtected(30); // f = false
A.Set(30, 24); // sets element 30 to 24
A.Protect(30);
f = A.IsProtected(30); // f = true
A.Set(30, 25); // nothing happens
}