5 Replies - 14421 Views - Last Post: 15 March 2009 - 02:11 PM Rate Topic: -----

#1 jmckennon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 15-March 09

Printing elements of a vector

Posted 15 March 2009 - 01:44 PM

Hi everyone,
I'm new here, this is my first post. In my programming class I am designing a simple text based video game. It's going very well, but I'm having trouble getting the game to display the contents of a vector. Pretty much, the user maneuvers through rooms. Every room has a treasure. There's a thief roaming around the map that steals treasure. WHen entering a room, the user has the option to pick up the treasure in the room and store it. I am implementing this via a vector, and having it store the name of the item in this vector. When the thief steals an item, the user has the option to view their inventory. To do this, I want to print the elements of the vector, but it keeps giving me the error :
 1>c:\users\justin\documents\visual studio 2008\projects\room\room\adventureproject.cpp(170) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>,
1>			_Ax=std::allocator<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(700): or	   'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(738): or	   'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(785): or	   'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(909): or	   'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(916): or	   'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(923): or	   'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(930): or	   'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(170): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(176): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(183): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(190): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(210): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(243): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(263): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 int)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(288): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned int)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(308): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(328): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned long)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(349): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(369): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(390): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(410): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(430): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(450): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files\microsoft visual studio 9.0\vc\include\ostream(470): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		while trying to match the argument list '(std::ostream, std::basic_string<_Elem,_Traits,_Ax>)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>,
1>			_Ax=std::allocator<char>
1>		]



which is extremely confusing. My code for this piece looks like
if(thiefIndex==roomIndex)
		{
			treasureList.pop_back();
			cout<<" A thief has stolen some of your treasure!"<<endl;
			cout<<" Would you like to view your inventory? (y/n)  "<<endl;
			cin>> choice;
			switch (choice){
				case 'y':
					cout<<endl;

					for( iter = treasureList.begin(); iter != treasureList.end(); iter++ ) {
						cout << *iter << endl;
					}
					break;
				case 'n':
					cout<< " You scream obscenities as loud as you can."<<endl<<" No one hears you";

			}


		}

I have my iterator declared as vector<string>::iterator iter; in the declarations. Why won't it print out?

Any help is greatly appreciated!! J

Is This A Good Question/Topic? 0
  • +

Replies To: Printing elements of a vector

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Printing elements of a vector

Posted 15 March 2009 - 01:51 PM

I'm unsure if this'll answer your question or not, but here's a working example of outputting values from a vector:

#include <iostream>
#include <vector>
using namespace std;

int main ()
{
  vector<int> myvector (10);   // 10 zero-initialized ints
  unsigned int i;

  // assign some values:
  for (i=0; i<myvector.size(); i++)
    myvector.at(i)=i;

  cout << "myvector contains:";
  for (i=0; i<myvector.size(); i++)
    cout << " " << myvector.at(i);

  cout << endl;
    cin.get();
  return 0;
}


EDIT: Dumb me... welcome to DIC! :D

This post has been edited by Hyper: 15 March 2009 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 jmckennon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 15-March 09

Re: Printing elements of a vector

Posted 15 March 2009 - 01:59 PM

View PostHyper, on 15 Mar, 2009 - 12:51 PM, said:

I'm unsure if this'll answer your question or not, but here's a working example of outputting values from a vector:

#include <iostream>
#include <vector>
using namespace std;

int main ()
{
  vector<int> myvector (10);   // 10 zero-initialized ints
  unsigned int i;

  // assign some values:
  for (i=0; i<myvector.size(); i++)
    myvector.at(i)=i;

  cout << "myvector contains:";
  for (i=0; i<myvector.size(); i++)
    cout << " " << myvector.at(i);

  cout << endl;
    cin.get();
  return 0;
}


EDIT: Dumb me... welcome to DIC! :D




I tried integrating your suggestion into my code, but it gave me the exact same 10 page error message as before. Any other help would be GREATLY appreciated, as I can't go much further until I figure this out.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Printing elements of a vector

Posted 15 March 2009 - 02:03 PM

Are you including all of the appropriate headers/libraries? If its a STL container of strings you need <string>
Was This Post Helpful? 0
  • +
  • -

#5 jmckennon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 15-March 09

Re: Printing elements of a vector

Posted 15 March 2009 - 02:09 PM

Yup, that did it. I forgot to include the string library! thanks!!
Was This Post Helpful? 0
  • +
  • -

#6 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Printing elements of a vector

Posted 15 March 2009 - 02:11 PM

Don't forget to use the "This Post Was Helpful!" button - congrats on getting it to work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1