Zipadoo

Could someone please tell me what's wrong her? Just learning classes.

1
2
3
4
5
6
7
8
9
10
11
12
13
class History{
public:
	string name;
	int rank;
};

int main()
{
	vector<History> historyVector;
	historyVector.rank.push_back(10);
	cout << historyVector[0].rank;
	return 0;
}
This line makes no sense whatsoever: historyVector.rank.push_back(10);
How to fix it depends on what you are trying to do.
Well I'm trying to make historyVector[0].rank equal to 10.
Before you can change any members of the first element, you have to add one first.
1
2
historyVector.push_back(History());
historyVector[0].rank=10;


Of course, providing an appropriate constructor would make this less ugly.
Well now I'm confused. See I don't actually want to set historyVector[0].rank to 10 necessarily, I want to push it back whatever the variable may be so that historyVector[0].rank becomes whatever the number is. Am I just missing this concept?
Uh, you want rank to correspond to the element's index in the vector? So in this case, rank should be 0?
Well since u have created a vector of a class. U have to push_back the class and then set the value for historyvector.

Historyvector[0] = container in array.
.rank = 10 = set the value of the array to 10.

Just as the guy above me said.

If it wasnt a class in ur vector though. Then this would be an correct code.

For example:

vector<int> vec;
vec.push_back(10);
cout<<vec.at(0);



You are confusing the vector with the elements of the vector. History has a member called rank and historyVector has an method called push_back.

Based on the code you have so far, you must:
1. Add rank to a History object
2. Add a history object to the vector

These can actually be done in either order. Athar showed the history object being added to the vector first. You could also do:

1
2
3
History h;
h.rank = 10;
historyVector.push_back(h);


What you tried to do was call "push_back()" on the "rank" member of historyVector. Since historyVector does not have a member named "rank" and the only "rank" in your code is an int (and thus does not have a method "push_back"), the whole line is non-sensical.
Alright I got it. Thanks for the help!
Topic archived. No new replies allowed.