creating multiple objects and storing them in a list

Pages: 12
I've tried that and I'm getting the error: non-standard syntax; use '&' to create a pointer to member.

This obviously means my getter functions are incorrect:
1
2
3
4
5
6
7
8
9
10
11
12
13
int Point::getX()
{
	return x;
}

int Point::getY()
{
	return y;
	
}

I've also tried 
//Coordinate class is renamed Point in this example 


1
2
3
4
5
6
Point Point::getX()
{
	return x; //no suitable constructor exists
                     //to convert from 'int' to 'coordinates'  
                     //how do I solve this?
}




What do I need to change? Thanks
Last edited on
Here's a complete working example.

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
#include <iostream>
#include <vector>

using namespace std;

class Coordinate
{
  int x,y;
public:
  Coordinate(int input_x, int input_y) : x(input_x), y(input_y){}
  int getX() {return x;}
  int getY() {return y;}
};

class octagon
{
  vector<Coordinate> lots_of_points;
public:
  void addCoord(Coordinate coord) {lots_of_points.push_back(coord);}
  Coordinate getCoord(int index){return lots_of_points[index];}
};

int main()
{
    octagon oct;
    
    Coordinate some_point(1,2);
    oct.addCoord(some_point);
    Coordinate another_point(50,400);
    oct.addCoord(another_point);
    
    cout << oct.getCoord(0).getX() << ", " <<  oct.getCoord(0).getY() << endl;
    cout << oct.getCoord(1).getX() << ", " <<  oct.getCoord(1).getY() << endl;
}
@Repeater, thank you so much! That cleared everything up now.
Topic archived. No new replies allowed.
Pages: 12