How to "push" points to a list

• 09-21-2007
Hunter_wow
How to "push" points to a list
Hello. I would like to know how I set in Points(x,y) to a list. Like this:

Code:

```#include <list> //Defining point type typedef struct Point{         int x, y; }Point; using namespace std; list<int> lista; lista.push_back(???);```
Any ideas how it should look like?
• 09-21-2007
Salem
Maybe
Code:

```list<Point> lista; Point foo; lista.push_back(foo);```
• 09-21-2007
Hunter_wow
I´m sorry. I started with C++ very recently and dont know how to handle the typedef. Have tried all I can come up with but it still dosent work. What would you set "foo" to for example?
Just some things I have tried:
Code:

```Point 1.x; Point 1 2; Point 1; Point (1,2); Point (1.x,2.y);```
and so on. Any clue? =)
• 09-21-2007
matsp
You could probably do
Code:

`lista.push_back(Point(1, 2));`
Or you can do:
Code:

```Point foo(1,2); lista.push_back(foo);```
--
Mats
• 09-21-2007
Hunter_wow
Code:

`Point foo(1,2);`
gives:
Code:

```error C2078: too many initializers error C2440: 'initializing' : cannot convert from 'int' to 'Point'```
and
Code:

`lista.push_back(Point(1, 2));`
gives:
Code:

`error C2661: 'Point::Point' : no overloaded function takes 2 arguments`
My typedef is a bit odd for a point. Guess the best would be to change it maybe? Tried:

Code:

```typedef struct Point{         (int x, int y); }Point;```
But your suggestions still gives errors =(
• 09-21-2007
CornedBee
If the struct is just a struct, the initialization is
Code:

```Point foo = { 1, 2 }; lista.push_back(foo);```
• 09-21-2007
matsp
Ah, ues, of course. You need a constructor for Point to do that.

Either do:
Code:

`Point foo = { 1, 2 };`
or
Code:

```struct Point{         int x, y;         Point(ax = 0, ay = 0): x(ax), y(ax) {}; }; Point foo(1, 2);```
Note that in C++ the typedef struct Point {} Point is unneccesary, you can use Point straight from the struct declaration, the typedef is implicit.

--
Mats
• 09-21-2007
Hunter_wow
Tons of thanks. Seems to work great =D
• 09-21-2007
Hunter_wow
Just one more small question: is it possible to get the x and y values from this?

Code:

```Point foo = { 1, 2 }; list.push_back(foo); // tried this but dosent seems to be the correct way int x = (list.pop_back()).x;```
edit: solved with
int x = lista.back().x;