CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Vector problem

  1. #1
    Join Date
    Jan 2008
    Posts
    2

    Vector problem

    I have problem with adding object into vector
    her's the code

    #include <iostream>
    #include <vector>

    using namespace std;

    class Pair {
    public:
    Pair(int a, int b)
    {x=a; y=b;};
    int get_x()
    {return x;};
    int get_y()
    {return y;};
    private:
    int x;
    int y;};

    int main()
    {vector<Pair> set();
    Pair* a1 = new Pair(70, 64);

    set.push_back(a1);


    system("PAUSE");
    return 0;
    }

    and i get only error message :S
    pls some help

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Vector problem

    You have a vector<Pair>, not a vector<Pair*>, so you should write:
    Code:
    vector<Pair> set;
    set.push_back(Pair(70, 64));
    Okay, actually, I lied. This declares a function named set that takes no arguments and returns a vector<Pair>:
    Code:
    vector<Pair> set();
    but I inferred that you actually wanted a vector<Pair> named set, and thus suggested the change accordingly.

    By the way, the C++ standard library already has std::pair.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Vector problem

    Flixter, do not call your variable "set". There already is a std::set container, and calling your variables the same as standard classes will just confuse you and/or the compiler.

    Regards,

    Paul McKenzie

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured