Thread: How to "push" points to a list

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    17

    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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Maybe
    Code:
    list<Point> lista;
    Point foo;
    lista.push_back(foo);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    17
    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? =)

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    17
    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 =(

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If the struct is just a struct, the initialization is
    Code:
    Point foo = { 1, 2 };
    lista.push_back(foo);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    17
    Tons of thanks. Seems to work great =D

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    17
    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;
    Last edited by Hunter_wow; 09-21-2007 at 06:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM