Question about classes‏

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];
};

CElement::CElement(void){
pos=rand()%10;
placeElement();
}

CTable::CTable(void){
int i;

for(i=0; i<10; i++){
vector[i]='0';
}
}

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.

Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <stdlib.h>

using namespace std;

class CElement
{
	int pos;
public:
	CElement(void);
	void placeElement(void);
};

CElement::CElement(void)
{
	pos=rand()%10;
	placeElement();
}

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
} 


class CTable
{
public:
	CElement element1, element2;
	CTable(void);
	char vector[10];
};

CTable::CTable(void)
{
	int i;
	for(i=0; i<10; i++){
	vector[i]='0';
}
}	//???

int main(int argc, char *argv[]){
	CTable table; 
	system("PAUSE"); 
	return 0;
} 


error is (atleast 1 i found shorly) on line 22 (Yeah i did modify a little of your code) CElement class has no member of vector ;)

edit: "modify" = rearrange
edit2: i did NOT fixed it; it's up to you :P (this will be learning)
Last edited on
Hi eraggo,

Thanks for your answer.

You are rigth, CElement class has no member of vector because that is not what I want to do.

the vector shown in line 22 is member of CTable;
But CElement is acting as if it does have a member called vector, on line 22 (in eraggo's reformatted version of your code).

Andy

P.S. You understand how to access class members from outside of a class?
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.

Thanks
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!

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <stdlib.h>

using namespace 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;
}
Last edited on
Topic archived. No new replies allowed.