Hi there,
I am learning how to program in c++ and I have a problem that I have not been able to solve. Since I am a beginner, chances are that it is a very stupid question, if so, please be kind with this poor amateur.
I have the following code, when I try to compile it I get an error saying: 'vector' undeclared.
#include <iostream>
#include <stdlib.h>
using namespace std;
class CElement{
int pos;
public:
CElement(void);
void placeElement(void);
};
class CTable{
public:
CElement element1, element2;
CTable(void);
char vector[10];
};
void CElement::placeElement(void){
vector[pos]='1'; //The error points to this line
//I trayed CTable.vector[pos]='1'; but it did not work aither
}
int main(int argc, char *argv[]){
CTable table;
system("PAUSE");
return 0;
}
My objective is to have an object called table that has 3 members (vector, element1 and element2). When element1 and element2 are created, they will place them self (randomly) into the vector.
If somebody could help me I will realy appreciate it.
Since I have not been able to get this simple code to compile, I do not think that I understand how to access class members from outside of a class.
What I need is:
From the constructor of the class Celement, modify 'vector' which is a member of the class CTable. Please note that objects element1, element2 are instances of the class CElement and they are members of the class CTable.
Given that the elements are members of the Table, you could give CElement a CTable& member. This would allow the elements to communicate with their parent table. This is not that uncommon an approach in this kind of situation. (The alternative would be to pass a ref to the table to the tables when they need it.)
Note that references must be init in the constructor list, which is before the body of constructor is run. So I moved the call to placeElement() out of the CElement construtor, as the index entry it set would have been overwritten by the constructor code. placeElement() is now called by the CTable constructor.
Also added a new method -- setElemPos() -- to set vector entry; it's bad form to access another classes data directly!
#include <iostream>
#include <stdlib.h>
usingnamespace std;
class CTable; // forward definition
// All class definitions
// Element
class CElement
{
CTable& table;
int pos;
public:
CElement(CTable& t);
void placeElement(void);
};
// Table
class CTable
{
char vector[10];
CElement element1, element2;
public:
CTable(void);
void setElemPos(int pos);
};
// Then class implementations, as methods ust
// now class definitions to be able to call
// methods.
// Element
CElement::CElement(CTable& t) : table(t)
{
}
void CElement::placeElement(void)
{
pos=rand()%10;
table.setElemPos(pos);
}
// Table
CTable::CTable(void)
: element1(*this), element2(*this)
{
for(int i=0; i<10; i++){
vector[i]='0';
}
element1.placeElement();
element2.placeElement();
}
void CTable::setElemPos(int pos)
{
vector[pos]='1'; //The error points to this line
//I trayed CTable.vector[pos]='1'; but it did not work aither
}
int main(int argc, char *argv[]){
CTable table;
system("PAUSE");
return 0;
}