Vector and Output help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;

void getItems(vector<string> bag){
	cout << endl << "Your items: \n";
	for(int i=0; i < bag.size(); ++i){
		cout << bag[i] << endl;
	}
	return;
}

int _tmain(int argc, _TCHAR* argv[])
{
	vector<string> inventory;
	inventory.push_back("sword");
	inventory.push_back("armor");
	inventory.push_back("shield");	
	int i=0;
	
	cout << "You have " << inventory.size() << " items in your inventory.";
	for(int i=0; i < inventory.size(); ++i){
		cout << inventory[i] << endl;
	}
	
	
	
	
	system("pause");
	return 0;
}


That code is my full code thus far. I'm new to C++ and I'm following along in the book "Beginning C++ Through Game Programming" by Michael Dawson. I'm currently starting chapter 4 on vectors but there is a problem compiling for my IDE (MS visual c++ 2010 express). The following code contains the problem according to the IDE:
1
2
3
for(int i=0; i < bag.size(); ++i){
		cout << bag[i] << endl;
	}

Specifically, the first "<<" after the "cout" says "Error: no operator "<<" matches these operands"

I commented out the "#include <vector>" statement and the "<<" error went away.

If anyone would be able to tell me what's going on, that'd be awesome.
Last edited on
That code shouldn't generate any errors, though you didn't show your full code. Can we see everything, including the #include s? There shouldn't be a semi-colon at the end of an #include directive (or any other pre-processor directive for that matter, unless you place it at the end of a #define for a specific purpose but that's not recommended).
Last edited on
Oh, wow I forgot to include those when I copied. Sorry about that. I have edited my first post.
I don't see #include <string>
Ah, well that would do it. That's embarrassing. Thank you for your help though, it works now.
Topic archived. No new replies allowed.