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.
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?
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.
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.
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.
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
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.
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.