accessing an object from a vector

How would I be able to access x or y variables from the octagons vector after storing the object.

I know how I'd access x and y from the lots_of_coordinates vector as this would just be lots_of_coordinates[0].x but I would want to do the same but from the octagons vector.

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
class Coordinate {
public:
	int x, y;
};

class OctagonBase {
public:
	vector<Coordinate>coordinates;
	Point point1;
};

class Octagon : public OctagonBase {

public:
	void Coordinates() {
		point1.x = 30;
		point1.y = 40;
		coordinates.push_back(point1);
		cout << coordinates[0].x;
	}


};

int main() {

	vector<Octagon*>octagons;

	Octagon o;
	o.setCoordinates();
	octagons.push_back(o);

	//how do I access x from octagons?	
}



edit:

I've got it to work using octagons[0].point1.x; and also changing vector<Octagon*>octagons to vector<Octagon>octagons. However is there a way to do it with vector<Octagon*>octagons.

Also if I change int x and y to private, would it still work if I add the appropriate setter and getter functions?
Last edited on
Hello rts17893,

DO NOT replace code in the OP (Original Post) This is confusing to those that come after the change and can not understand what you want because you have corrected or updated your code or message.

Even with the changes you have made, some for the better, the code still does not compile.

on line 9 you are using something called "Point", but where did that come from? Or am I missing a header file that you did not bother to mention in either code.

Line 30 is trying to call a function that does not exist.

I believe that you need to read up on classes because you are not using what you have in the best way.

This may help:
http://www.cplusplus.com/doc/tutorial/classes/
http://www.cplusplus.com/doc/tutorial/templates/

Or
https://www.learncpp.com/ Look at chapter 8.

When it does compile and work I believe what you want is octagons[index]->something. I think the "something" would come to be [unknown].x, but since the program does not compile I am not sure if this works.

Hope that helps,

Andy
Sorry I've written a simplified version of my actual code so I forgot to remove/edit the irrelevant parts of the code - hopefully the code below should compile.

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
class Coordinate {
public:
	int x, y;
};

class OctagonBase {
public:
	vector<Coordinate>coordinates;
	Coordinate point1;
};

class Octagon : public OctagonBase {

public:
	void Coordinates() {
		point1.x = 30;
		point1.y = 40;
		coordinates.push_back(point1);
		cout << coordinates[0].x;
	}


};

int main() {

	vector<Octagon*>octagons;

	Octagon o;
	o.Coordinates();
	octagons.push_back(o);

	//how do I access x from octagons?	
}


Using octagons[0]->point1.x instead does work when replacing octagons.push_back(o); with octagons.push_back(new Octagon) however 0 is printed to the console rather than the expected 30

Last edited on
Hello rts17893,

Sorry I missed you edit. Another problem of changing your OP.

Since everything in your clasess is public the whole program has access to everything. "get" and "set" functions are public functions used to access the "private" variables and functions of a class.

I see new code. I will give it a try shortly.

Andy
Hi Andy,

Sorry another mistake - I meant if I change x and y to private as they are currently public.

Thanks
Hello rts17893,

Yes. Then you would need the "get" and "set" functions.

Working on the program I did have to change line 31 to octagons.push_back(&o);, but what I thought would work did not and I ended up with std::cout << "\n " << o.point1.x << "\n " << o.point1.y << '\n';.

I do not often work a vector of pointers, so I was not sure what would work untio I could try it.

Hope that helps,

Andy
Why do you want vector of pointers?

with octagons.push_back(new Octagon) however 0 is printed to the console rather than the expected 30

Why do you expect 30 from that?

The
1
2
3
Octagon* aa = new Octagon; // construct object
std::cout << aa->point1.x; // read object data
delete aa;

is different from
1
2
3
4
Octagon* aa = new Octagon; // construct object
aa->Coordinates(); // modify object
std::cout << aa->point1.x; // read object data
delete aa;

Last edited on
Topic archived. No new replies allowed.