Class and <vector>

This is a discussion on Class and <vector> within the C++ Programming forums, part of the General Programming Boards category; Let's say I have the following code: Code: class myClass { private: int x, y; public: myClass(); myClass(int t_x, int ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Question Class and <vector>

    Let's say I have the following code:
    Code:
    class myClass {
      private:
        int x, y;
      public:
        myClass();
        myClass(int t_x, int t_y) { x = t_x; y = t_y; }
       ~myClass();
    };
    
    myClass aClass;
    vector<myClass> vClass;
    Ok, now here's what I am trying to accomplish. I have this loop with a keypress condition check. When the user presses the key, I want to push_back an entry to vClass with a set x and y value. The closest I got was a miserable crash with no error message when I pushed the key.

    No need to worry about the key condition stuff since I got that taken care of already. I just need some help on how to manage with the vector as described.

    Something like this, but of course it does not work:
    Code:
    vClass.push_back(aClass(2, 4))
    If this could done more efficiently, great.

    Thanks.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,673
    This should work:
    Code:
    vClass.push_back(myClass(2, 4));
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,805
    >vClass.push_back(aClass(2, 4))
    aClass is an object, not a class. When you do this, the compiler searches for an overloaded () operator because you are effectively trying to call an object as a function. Because you don't have operator() in your class definition, it won't work. I think you really want to create a new object and store it in the vector, so you would want to call a constructor instead:
    Code:
    vClass.push_back(myClass(2, 4));
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Thumbs up

    Quote Originally Posted by hk_mp5kpdw
    This should work:
    Code:
    vClass.push_back(myClass(2, 4));
    Thanks hk_mp5kpdw, worked like a charm. Thank you for the explaination Prelude, I can see why it boogered out on me now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed of <queue> and <vector>
    By scwizzo in forum C++ Programming
    Replies: 7
    Last Post: 05-12-2009, 02:32 PM
  2. how to pass function pointer as arg in c++
    By umen242 in forum C++ Programming
    Replies: 13
    Last Post: 10-21-2008, 02:09 AM
  3. Shifting back <vector> items...
    By aker_y3k in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2003, 10:30 AM
  4. Implementation of <vector> class
    By supaben34 in forum C++ Programming
    Replies: 4
    Last Post: 10-13-2002, 09:14 AM

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