I have some problem with putting object in 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 error message :S

Recommended Answers

All 4 Replies

Many problems in the code.. but this will remove the error:

#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;
}

- Dont use system("pause")
- Format your code better
- Use code tags

I have some problem with putting object in 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 error message :S

Two problems. See lines 19 and 22. One, on line 19, get rid of the parentheses. set is not a function. Two, in line 19, you define set as a vector of Pair . In line 20, you define a1 as a pointer to Pair . These two types do not match, so you must dereference a1 with the * operator and make it type Pair in line 22 before you can push it onto the vector. See corrected code below.

#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;
}

[EDIT] Someone beat me to it! :angry:

commented: Heh, unlucky :) +2

I have formatted the code for you and fixed it to the way it should have been done. :icon_neutral:

#include <iostream>
#include <vector>

using namespace std;

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

int main()
{
	vector<Pair> set;

	Pair a1(70, 64);
	set.push_back(a1);

	cin.ignore();
	return 0;
}

Hope it helps.

Tnx a lot problem fixed ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.