Problem with structures.

I was looking through the data structure tutorial and I thought I will do my own example for structures, but it gives the following errors. Even the sample program that is given in the website is giving the same list of errors...I have attached the code and the errors for your easy reference...thanks for the help...

//structures programming

#include<iostream>
#include<sstream>//this is to read strings from the command line and use them as input.
#include<string>//this is to do all sorts of matter with strings.

using namespace std; //struct declaration and all comes after this only.

struct branch{//branch is the name of the structure, from now on it will be treated like a datatype like int or float
string type;
int hits,misses;//hits and misses are members of the structure and they are of datatype branch
}mine,yours;//these are the objects of datatype product.

void printing(branch method)//the d.type can be used in functions too.
{
cout<<"hits:"<<method.hits;
cout<<"misses:"<<method.misses;
}

int main()
{
string mystr;//sh: u will have to do this because this is whre the string that is read from the CLI is temporarily stored.
branch gshare;//i have done this jus to show tht a var of type branch can also be declared like this
cout<<"enter the type of branch prediction"<<endl;

getline (cin,mystr);
stringstream(mystr) >> yours.type;

//getline(cin,mystr);//sh: u made a mistake here. It is just getline...no cin b4 and all...
//stringstream(mystr) >> yours.type;//NOTE THIS COMMAND, U GOT IT WRONG
cout<<"shiva, enter your type of branch predictor"<<endl;
getline(cin,mine.type);//showing another method of reading from the command line.
if((mine.type=="gag") || (yours.type=="gag"))
{
mine.hits=100;
yours.hits=100;
mine.misses=100;
yours.misses=100;
}
/*if(mine.type=="gap" || yours.type=="gap")
{
mine.hits=yours.hits=110;
mine.misses=yours.misses=90;
}
if(mine.type=="pag" || yours.type=="pag")
{
mine.hits=yours.hits=120;
mine.misses=yours.misses=80;
}
if(mine.type=="pap" || yours.type=="pap")
{
mine.hits=yours.hits=130;
mine.misses=yours.misses=70;
}*/
printing(yours);
printing(mine);
return 0;
}

errors:
structures.cc: In function ‘int main()’:
structures.cc:27: error: no match for ‘operator>>’ in ‘std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >(((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& mystr))), std::operator|(_S_out, _S_in)) >> yours.branch::type’
/usr/include/c++/4.3/istream:123: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:127: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:134: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:170: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:174: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:177: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:181: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:184: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:188: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:192: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:197: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:201: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:206: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:210: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:214: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:218: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:242: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/bits/basic_string.h:2407: note: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]

Please read compiler's report and fix all errors that it generates.
you could always do what it suggests and try using istream. Personally I can't see what is wrong with it, but it is extremely hard to read without code tags, you should use these in future, the button that looks like <>

you could also do: yours.type = mystr;
Last edited on
I've just tried your code in my compiler (MS Visual C++ 2008) and it compiles without errors.

It seems to be some compiler-specific error. What compiler do you use?
Topic archived. No new replies allowed.