Vectors and Windows Programming

This is a discussion on Vectors and Windows Programming within the Game Programming forums, part of the General Programming Boards category; Hi, Is it possible for me to make a vector of POINT's Something like: Code: vector<POINT> name; Thanks...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Exclamation Vectors and Windows Programming

    Hi,

    Is it possible for me to make a vector of POINT's

    Something like:

    Code:
    vector<POINT>     name;

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see any reason that wouldn't work. POINT is a simple struct, so that should work just fine in a vector.

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

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Quote Originally Posted by matsp View Post
    I don't see any reason that wouldn't work. POINT is a simple struct, so that should work just fine in a vector.

    --
    Mats
    Thats what I thought but I'm getting 2 errors about that line.

    Error1: error C2143: syntax error : missing ';' before '<'
    Error2: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

    :-S

    FYI:

    Code:
    #include <windows.h>
    #include <vector>
    are also present.

    Any ideas?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you by any chance missing a "std::" in front of "vector"?

    --
    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
    Nov 2006
    Posts
    224

    Wink


    Legend

    Nice 1

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    How do I write to a field of the struct?

    eg)
    Code:
    name.x.push_back(23)
    ??

    How do you access a vector op and a struct field at the same time??

    Thanks

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, you'd push back a POINT, e.g.
    Code:
    POINT p;
    p.x = 23;
    p.y = 42;
    v.push_back(p);
    After all, you have a vector of points, not a vector of x's and another vector of y's.

    --
    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
    Nov 2006
    Posts
    224
    Quote Originally Posted by matsp View Post
    No, you'd push back a POINT, e.g.
    Code:
    POINT p;
    p.x = 23;
    p.y = 42;
    v.push_back(p);
    After all, you have a vector of points, not a vector of x's and another vector of y's.

    --
    Mats
    Thanks

    I get your POINT



    wuldnt it be alot better for me to create an array of points then which I can acvcess much easier:

    Code:
    name[0].x = 23
    What do you think?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can do that with vector too: Use the "presize" by construction:
    Code:
    std::vector<T> name(size);
    Or, you can resize the vector to the size you want:
    Code:
       name.resize(size);
    --
    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.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Thumbs up

    Quote Originally Posted by matsp View Post
    You can do that with vector too: Use the "presize" by construction:
    Code:
    std::vector<T> name(size);
    Or, you can resize the vector to the size you want:
    Code:
       name.resize(size);
    --
    Mats
    ahh! excellent

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Quote Originally Posted by matsp View Post
    No, you'd push back a POINT, e.g.
    Code:
    POINT p;
    p.x = 23;
    p.y = 42;
    v.push_back(p);
    After all, you have a vector of points, not a vector of x's and another vector of y's.

    --
    Mats
    So if i wanna add extra data as and when its needed I am going to have to follow the protocol (above)?

    Because that is painful

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by strokebow View Post
    So if i wanna add extra data as and when its needed I am going to have to follow the protocol (above)?

    Because that is painful
    You can of course resize as you need it too. So if you know you need 10 more, you do:
    Code:
    size_t size = v.Size();
    size_t newsize = size + 10;
    v.resize(newsize);
    for(i = size; i < newsize; i++)
    {
       v[i].x = i;
       v[i].y = i * 2;
    }
    It's even more painful if you have to do this without vector (normal C style arrays and/or dynamic memory allocation).


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

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Does POINT have any constructors? Would it allow you to do this?
    Code:
    v.push_back(POINT(23,42));

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Thantos View Post
    Does POINT have any constructors? Would it allow you to do this?
    Code:
    v.push_back(POINT(23,42));
    No, POINT is a standard C type, something like this:
    Code:
    typedef struct _POINT
    {
       int x;
       int y;
    } POINT;
    It may use a different integral type (guessing DWORD), but in essence it doesn't have any C++ features.

    I guess we could make a proxy object that pretends to be a POINT.

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

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Quote Originally Posted by Thantos View Post
    Does POINT have any constructors? Would it allow you to do this?
    Code:
    v.push_back(POINT(23,42));
    Nice thought. But,

    error C2661: 'tagPOINT::tagPOINT' : no overloaded function takes 2 arguments

    POINT is just a structure

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors, with vectors, windows programming... etc
    By bobbelPoP in forum C++ Programming
    Replies: 10
    Last Post: 07-27-2008, 04:07 AM
  2. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 01:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21