C++/VB - error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
Asked By aarthi2
17-Jun-07 06:12 AM

Hi,
I have written this code, and at the end, I am trying to write a
vector of strings into a text file. However, my program is nor
compiling, and it gives me the following error when I try to write to
the file:
error C2679: binary '<<' : no operator found which takes a right-hand
operand of type 'std::string' (or there is no acceptable conversion)
I don't know what I am doing wrong. I have posted my entire program
here.
Thank you
using namespace std;
char n_str[2000];
char a_str[2000];
char n_char[2000];
char a_char[2000];
string achar, nchar;
int main(int argc, char* argv[])
{
vector<string> n_word_list;
vector<string> a_word_list;
ifstream in_a("10_a.txt");
if (!in_a)
{
cout << "Error opening abnormal file" << endl;
}
while (!in_a.eof())
{
in_a.getline(a_str,2000);
a_word_list.push_back(a_str);
}
cout << a_word_list.size()<< endl;
ifstream in_n("10_n.txt");
if (!in_n)
{
cout << "Error opening normal file" << endl;
}
while (!in_n.eof())
{
in_n.getline(a_str,2000);
n_word_list.push_back(a_str);
}
cout << n_word_list.size()<< endl;
for (unsigned int i=0; i<a_word_list.size(); i++)
{
for (unsigned int j=0; j<n_word_list.size();j++)
{
if (a_word_list[i].compare( n_word_list[j]))
{
a_word_list.assign(1,"aa");
n_word_list.assign(1,"aa");
}
}
}
ofstream out_a("10_a_new.txt");
if (!out_a)
{
cout << "Error opening new abnormal file" << endl;
}
for (unsigned int i=0; i<a_word_list.size(); i++)
{
achar = a_word_list.at(i);
out_a << achar << endl; //ERROR
}
ofstream out_n("10_n_new.txt");
if (!out_n)
{
cout << "Error opening new normal file" << endl;
}
for (unsigned int i=0; i<n_word_list.size(); i++)
{
out_n << n_word_list.at(i) << endl; //ERROR
}
return 0;
}
Boekelheide
(1)
Providenza
(1)
Ifstream
(1)
Ofstream
(1)
Operand
(1)
Streams
(1)
VC2005
(1)
Vector
(1)
David Lowndes replied...
Try adding .c_str():
out_a << achar.c_str() << endl;
Dave
Alex Blekhman replied...
You forgot #include <string>.
Alex
Tim Roberts replied...
OK, I admit I am confused. I cut and pasted his code to my local PC, and
in fact you are correct: #include <string> fixes it.
However, long before he tries to write the string to cout, we see this:
string achar, nchar;
int main(int argc, char* argv[])
vector<string> n_word_list;
vector<string> a_word_list;
all of which compiles successfully. Further, the error message (shown
above) clearly shows that the compiler knows that "string" means
So, the compiler has PART of std::string, but not all?
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Alex Blekhman replied...
Yes, other standard headers can include <string> or forward
declare `std::string' for internal purposes. However, you
should not rely on it. In your specific case, `operator <<'
that writes `std::string' into a stream is declared inside
Alex
David Wilkinson replied...
Alex:
Without looking at the headers in detail, I would have to say that I am
still confused (for the same reason as Tim). But clearly you are
correct, <string> is missing.
This issue of missing headers is the single most common problem I find
in cross-platform work. I develop on VC7.1 and compile the same code
with gcc, and I quite often get missing header errors because VC has
allowed me to omit some header because (presumably) it is included in
some other header. Of course, the resolution is always to include the
correct headers, but this is not so easy when the code compiles without
them.
--
David Wilkinson
Visual C++ MVP
Alex Blekhman replied...
I don't have VC7.1 to check this case, but VC2005 compiles
and links it without any problem.
Anyway, `basic_string' is implemented in <xstring> header,
actually. Several standard headers include <xstring> either
directly or indirectly because of methods that return
`std::string' and/or accept it as a parameter. That's why
`std::string' is available in user's code too when something
like <iostream> or <exception> is included. However, I/O
operators that work with `std::string' are declared in
streams one can get away with working code without including
Implementation of other standard classes can be scattered
among several headers, too. It gives a developer an illusion
as if compiler "knows" partially about some classes.
I agree. That's why I insist on nightly cross-platform build
when I work on cross-platform a project. It helps to
pinpoint such problems early.
Alex

line which gets saved in a .txt file called 'emailscan' - this is then read by ifstream and scanned for the chosen word which if found is returned with its location The The answer depends on how you are doing the reading. - - Tim Roberts, timr@probo.com Providenza & Boekelheide, Inc. using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string line, paragraph, text, textin The text has been saved in emailscan.txt" << endl; / / checkpoint / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / OPEN AND READ EMAILSCAN.TXT ifstream filereader("emailscan.txt"); / / first, read the text file if( !filereader) / / check that emailscan can be the past with the end-of-file character, Ctrl-Z. - - Tim Roberts, timr@probo.com Providenza & Boekelheide, Inc. keywords: C++, program, paste, paragraph, of, text description: Hello, I'm writing a C
buf << ' \ n'; cin.ignore(100, ' \ n'); } VC Language Discussions FeedbackID (1) OSGeo (1) VisualStudio (1) Boekelheide (1) AndStephan (1) Providenza (1) Lavavej (1) Stephan (1) This is why I do not like the iostream classes known and standard behaviour and Stephan T. Lavavej explains it extensively here: ID:351636 "STL: ifstream::getline() does not set eof flag" https: / / connect.microsoft.com / VisualStudio / feedback / ViewFeedback.aspx?FeedbackID the stream. Call cin.clear(); to clear the failbit condition. - - Tim Roberts, timr@probo.com Providenza & Boekelheide, Inc. keywords: Question, about, the, getline, method. . . description: Hello, The intent of the following code
work well, both for storing and accessing? Please advise. TIA VC Language Discussions Excel (1) Boekelheide (1) Providenza (1) Margarita (1) Copeland (1) Phoenix (1) Cities (1) Rancho (1) VC6 is ancient and gov / tiger / tms / gazetteer / zips.txt / / http: / / www.census.gov / tiger / tms / gazetteer / zip90r.txt ifstream f("zips.txt"); if (!f) { throw runtime_error("RUNTIME ERROR: Couldn't open zips.txt."); } const City, UT Inver Grove Heights, MN South San Francisco, CA - - Tim Roberts, timr@probo.com Providenza & Boekelheide, Inc. keywords: Fast, Access, of, Large, Data, Set description: I have a large data file
method1"; } const std::string method2( std::istream& stream_ ) { return methodDoesNotExist(); } / / * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * / / Tester.cpp / / * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * int main(){ std::ifstream stream; stream.open("testerParse.txt"); std::cout << ::method1( stream ) << std::endl; stream.close(); char c the code, and Turing gets in the way of that. - - Tim Roberts, timr@probo.com Providenza & Boekelheide, Inc. e very vision f es r s e n Tim, Thank you for the some of them are not actually used in the code. - - Tim Roberts, timr@probo.com Providenza & Boekelheide, Inc. keywords: Why, is, reference, from, unused, function, required? description: I have stumbled across a