string problem

Started by
22 comments, last by Rydis 19 years, 10 months ago
ok so i took out iostream and switched to <fstream>

C:\Documents and Settings\Jamie\Desktop\Game\Battle\battle.cpp(10) : error C2065: 'cout' : undeclared identifier
C:\Documents and Settings\Jamie\Desktop\Game\Battle\battle.cpp(10) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,const class std::basic_string<_E,_Tr,_A> &)' : could not deduce template
argument for 'class std::basic_ostream<_E,_Tr> &' from 'int'
C:\Documents and Settings\Jamie\Desktop\Game\Battle\battle.cpp(10) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,const short *)' : could not deduce template argument for 'class std::bas
ic_ostream<_E,_Tr> &' from 'int'
C:\Documents and Settings\Jamie\Desktop\Game\Battle\battle.cpp(10) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,const unsigned char)' : could not deduce template argument for 'class st
d::basic_ostream<_E,_Tr> &' from 'int'
C:\Documents and Settings\Jamie\Desktop\Game\Battle\battle.cpp(10) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,const unsigned char *)' : could not deduce template argument for 'class
std::basic_ostream<_E,_Tr> &' from 'int'
C:\Documents and Settings\Jamie\Desktop\Game\Battle\battle.cpp(10) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,const signed char)' : could not deduce template argument for 'class std:
:basic_ostream<_E,_Tr> &' from 'int'
C:\Documents and Settings\Jamie\Desktop\Game\Battle\battle.cpp(10) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,const signed char *)' : could not deduce template argument for 'class st
d::basic_ostream<_E,_Tr> &' from 'int'
C:\Documents and Settings\Jamie\Desktop\Game\Battle\battle.cpp(10) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,_E)' : could not deduce template argument for 'class std::basic_ostream<
_E,_Tr> &' from 'int'
C:\Documents and Settings\Jamie\Desktop\Game\Battle\battle.cpp(10) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,const _E *)' : could not deduce template argument for 'class std::basic_
ostream<_E,_Tr> &' from 'int'
C:\Documents and Settings\Jamie\Desktop\Game\Battle\battle.cpp(10) : error C2677: binary '<<' : no global operator defined which takes type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acc
eptable conversion)
Advertisement
<fstream> is not intended to replace <iostream>

Use <iostream>, (and not <iostream.h>) to get std::cin, std::cout and std::cerr.
Use <fstream>, (and not <fstream.h>) to get std::ifstream, std::ofstream and std::fstream.

#include <iostream>#include <string>using namespace std;int main(){  string Blah = "Ha";  cout << Blah;  return 0;}


Should work. Add #include <fstream> if you need file streams.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

OK, try this

#include <iostream>#include <fstream>#include <string>using namespace std;int main(){string Blah = "Ha";cout<<Blah;return 0;}


Edit: Man, that Fruny guy is fast isn't he? :P
You still need to include <iostream>

They were only warning you not to mix the *.h versions with the non .h versions.

EDIT: Wow, beaten twice...
that works thanks alot. just trying to move outta the character arrays and onto strings

i had fstream cause this is going to be a battle simulation. I had a text RPG going but friend moved. So now im gonan to try to make a text battle simulation to pratice using strings, file input and out put, and some AI techniques.
since already on subject.

How do i make it work with cin.getline()?

getting same type of error :(

can you not use it with strings or would i have to convert it.

#include <iostream>#include <fstream>#include <string>using namespace std;int main(){	string Character_Name;	cin.getline(Character_Name, 256, '\n');	cout <<Character_Name;	return 0;}


error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::get(char *,int,char)' : cannot conv
ert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
You need to use the std::getline() function that is in <string>
#include <iostream>#include <fstream>#include <string>using namespace std;int main(){	string Character_Name;	getline(cin, Character_Name);	cout <<Character_Name;	return 0;}


Unfortunately, the Visual Studio 6 library suffers from a bug which makes that function behave incorrectly. You can correct it (and others) by doing the changes specified on this page. Yes, it does involve manually changing code in the standard headers, but it's not such a daunting task.

Oh, and here's some error message translation help:
std::basic_istream<char,struct std::char_traits<char> > means std::istream
std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > means std::string
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
thanks alot really helpful.
the fix didn't stop the extra character needed to gain control again though.
Quote:Original post by Rydis
the fix didn't stop the extra character needed to gain control again though.


The <string> fix, where you replace snextc with sbumpc should have.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement