Click to See Complete Forum and Search --> : Vector problem


Flixter
May 31st, 2008, 02:05 PM
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

laserlight
May 31st, 2008, 02:12 PM
You have a vector<Pair>, not a vector<Pair*>, so you should write:
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>:
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.

Paul McKenzie
May 31st, 2008, 03:31 PM
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