Results 1 to 7 of 7

Thread: vector.size() of a custom class.

  1. #1
    Join Date
    Dec 2009
    Beans
    40

    vector.size() of a custom class.

    I'm trying to initialize this function in cpp:

    Code:
    class coord {
        int x;
        int y;
    public:
        coord(int a, int b);
    };
    coord::coord(int a, int b) {
        x = a;
        y = b;
    }
    
    void move(std::vector<coord> *vertex, int *map[21][12], char dir) {
        if (dir = 'D'){
                for (int i = vertex.size() ; i > 0; i--) {
    
                }
        }
    }
    get a compiler error

    In function ‘void move(std::vector<coord, std::allocator<coord> >*, int* (*)[12], char)’:
    error: request for member ‘size’ in ‘vertex’, which is of non-class type ‘std::vector<coord, std::allocator<coord> >*’|
    ||=== Build finished: 1 errors, 0 warnings ===|

  2. #2
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: vector.size() of a custom class.

    Think about what type of variable "vertex" is. Why would using *any* instance function on vertex be a bad idea?

  3. #3
    Join Date
    Dec 2009
    Beans
    40

    Re: vector.size() of a custom class.

    Quote Originally Posted by NovaAesa View Post
    Think about what type of variable "vertex" is. Why would using *any* instance function on vertex be a bad idea?
    Just thought of something -- could be wrong let me know.

    vertex is a pointer to a piece of memory that holds a vector, thus it's not a vector itself.

    So change *vertex to &vertex, which should work.
    Last edited by YourMomsASmurph; September 30th, 2010 at 02:38 AM.

  4. #4
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: vector.size() of a custom class.

    Yes, you could do that and it would work fine. Another option would be to change vertex.size() to vertex->size(). This is shorthand for (*vertex).size().

    EDIT: Which option is best really depends on what calling semantics you are after.

  5. #5
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: vector.size() of a custom class.

    Quote Originally Posted by YourMomsASmurph View Post
    I'm trying to initialize this function in cpp:

    Code:
    class coord {
        int x;
        int y;
    public:
        coord(int a, int b);
    };
    coord::coord(int a, int b) {
        x = a;
        y = b;
    }
    
    void move(std::vector<coord> *vertex, int *map[21][12], char dir) {
        if (dir = 'D'){
                for (int i = vertex.size() ; i > 0; i--) {
    
                }
        }
    }
    get a compiler error

    In function ‘void move(std::vector<coord, std::allocator<coord> >*, int* (*)[12], char)’:
    error: request for member ‘size’ in ‘vertex’, which is of non-class type ‘std::vector<coord, std::allocator<coord> >*’|
    ||=== Build finished: 1 errors, 0 warnings ===|
    Is that the real code, or something that you conjured up from memory?

    Error here:
    Code:
    if (dir = 'D'){    // assignment?
    Error here:
    Code:
    for (int i = vertex.size()   // vertex is a pointer
    Warning here:
    Code:
    for (int i = vertex.size()...   // size() returns type of size_t
    Now the following begs to be asked... why are you passing pointers around? Just pass by reference.

    And why is 'move(...)' not part of a class?

  6. #6
    Join Date
    Dec 2009
    Beans
    40

    Re: vector.size() of a custom class.

    Quote Originally Posted by dwhitney67 View Post
    Is that the real code, or something that you conjured up from memory?
    Came up with it from memory, the code is... huge right now, so just tried to remember the parts that were important.

    Quote Originally Posted by dwhitney67 View Post
    Is that the real code, or something that you conjured up from memory?
    Error here:
    Code:
    if (dir = 'D'){    // assignment?
    Error here:
    Code:
    for (int i = vertex.size()   // vertex is a pointer
    Warning here:
    Code:
    for (int i = vertex.size()...   // size() returns type of size_t
    Now the following begs to be asked... why are you passing pointers around? Just pass by reference.

    And why is 'move(...)' not part of a class?
    Brain wasn't working -- Thought I was passing by reference for what ever reason.
    It wasn't until NovaAesa made me look at the code that I realized what I was doing was completely wrong.

    //Making a "Falling block game" brains slightly lost as to how to organize it properly (Which is why move is not part of a class yet).
    I'm just creating all the functions I am going to need. Write each one separately, then put together and edit at the end.
    (Only really have 1 semester of programming courses[trying to self teach], BUT need to have a few games programmed by this summer to get a chance at a specific internship.)
    Last edited by YourMomsASmurph; September 30th, 2010 at 03:07 AM.

  7. #7
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: vector.size() of a custom class.

    By the way, I'm pretty sure you also don't need your map parameter to be an array of 2D arrays (int *map[12][12])... Pass the 2D array, unless you want to modify the 2D array's memory address (which I really doubt: I guess you want to modify its contents). But maybe, your map should be a class or maybe there is a STL container for that (I'm not a C++ programmer)... C-like arrays are impractical in C++ because C++ has better higher-level constructs for you (C has to cope with that kind of arrays because of what tasks the language is meant to be used for... but you're *not* programming in C!).
    Last edited by nvteighen; September 30th, 2010 at 07:31 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •