error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string'

A

aarthi28

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

#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <math.h>
#include "string.h"

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.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;
}
 
B

Baltimore

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

#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <math.h>
#include "string.h"

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.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;

}


Can you give your compiler version and the line of the error, please ?
 
I

Ian Collins

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.

It doesn't look like you are doing anything wrong, the code in question
looks fine to me.
 
A

aarthi28

I am using Visual Studio 2005, and I marked the two lines that are
giving me errors in my original post. I am re-posting the error
portion here.
Thank you

..
..
..
..
for (unsigned int i=0; i<a_word_list.size(); i++)
{
achar = a_word_list.at(i);
out_a << achar << endl; //ERROR
}

..
..
..

for (unsigned int i=0; i<n_word_list.size(); i++)
{
out_n << n_word_list.at(i) << endl; //ERROR
}
return 0;
..
..
..
 
A

aarthi28

Is there something else I can do to make this run? I don't know why I
am getting an error
 
R

Roland Pibinger

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

#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <math.h>
#include "string.h"

#include <string>
 
B

Bo Persson

(e-mail address removed) wrote:
:: 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
::
:: #include <iostream>
:: #include <fstream>
:: #include <iterator>
:: #include <algorithm>
:: #include <vector>
:: #include <math.h>
:: #include "string.h"
::
:: using namespace std;
::

The operators for std::string are found in the <string> header.


Bo Persson
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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

#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <math.h>
#include "string.h"

That does not look right to me. What does "string.h" contain? If you
wanted to use the C string header you should have used <string.h> or
<cstring>. However I suspect that you wanted to use C++ strings, in
which case you should use <string> (notice that none of the standard C++
headers have a .h when including). If you use <string> instead of
"string.h" your code will compiler just fine.
 
J

John Harrison

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

You are forgetting

#include <string>

No .h and <> not ""

john
 
J

James Kanze

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.

It really would have been better if you'd have created a minimum
example.
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <math.h>
#include "string.h"

As others have said, you haven't included <string>. So there's
no guarantee that you have all of the operations that are
normally provided for std::string.
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())

This line is wrong. What you want is actually:

std::string a_str ; // No point in reading into a C style
// array...
while ( std::getline( in_a, a_str ) ) {
a_word_list.push_back( a_str ) ;
}

[...]
while (!in_n.eof())

Same comment as above.
cout << n_word_list.size()<< endl;

And of course, this whole loop can be written as:

std::copy( a_word_list.begin(), a_word_list.end(),
std::eek:stream_iterator< std::string >( out_a,
"\n" ) ) ;
out_a.close() ; // Since you're not using it any more...
if ( ! out_a ) {
std::cerr << "Write error in out_a" << std::endl ;
}

As above.
for (unsigned int i=0; i<n_word_list.size(); i++)
{
out_n << n_word_list.at(i) << endl; //ERROR
}

Since (rather exceptionally for a beginner) you are doing
correct error checking, don't forget to check that your writes
actually worked.
 
D

Default User

Is there something else I can do to make this run? I don't know why I
am getting an error

Please include sufficient quotes from the previous message to provide
context for your message. Google Groups does this automatically now, so
you have no excuse.



Brian
 
D

Default User

Baltimore said:
Can you give your compiler version and the line of the error, please ?

If the first matters, then it's probably off-topic. For the second, he
flagged the lines with comments. You'd probably have noticed had you
gone through the post and trimmed down the quotes.



Brian
 
J

James Kanze

If the first matters, then it's probably off-topic.

Since when? Questions concerning the conformance of a compiler
are on topic. More importantly, the poster doesn't know the
answer to his problem; indicating the compiler version may allow
someone to say that it is just a compiler bug, and that his code
is correct, and save a lot of other people a lot of work trying
to figure out why the code doesn't work. In general, when
asking about a specific error message, indicating the compiler
and its version (and possibly the OS it's running on, in the
case of g++) is just considered correct posting etiquette.
 
D

Default User

James said:
Since when? Questions concerning the conformance of a compiler
are on topic.

They CAN be, but seldom are, which is why I said "probably".

It's not usually the first question that needs to be asked.



Brian
 
J

James Kanze

They CAN be, but seldom are, which is why I said "probably".

I'm sorry, but questions along the lines of: "The following code
doesn't compile. Is this a problem with my compiler, or is the
code actually illegal?" are perfectly on topic. Always.
It's not usually the first question that needs to be asked.

Yes and no. It's standard etiquette in this group to indicate
the compiler and the version when there is a problem, because it
can make answering the question easier. From the FAQ, question
5.7:

How do I post a question about code that doesn't work
correctly?

[...]

5. Post the tools you used: compiler name, version
number, operating system, etc

6. Post the tool options you used: libraries, exact
compiler and linker options, etc

7. Post the exact messages you received; differentiate
between compiler, linker, and runtime messages

It's bad enough when we have people complaining about things
that are marginal (when in doubt, allow it), but to complain
about following the guidelines in the FAQ...
 
B

Baltimore

Is there something else I can do to make this run? I don't know why I
am getting an error

Maybe, you define a 'string' type in the "string.h" header.
So the 'achar' and 'nchar' variables could be of your own string type.
Write std::string for this variables and include <string> to use the
string library of C++.
 
D

Default User

James said:
I'm sorry, but questions along the lines of: "The following code
doesn't compile. Is this a problem with my compiler, or is the
code actually illegal?" are perfectly on topic. Always.

That wasn't the question, was it?
Yes and no. It's standard etiquette in this group to indicate
the compiler and the version when there is a problem, because it
can make answering the question easier.

That's fine, and had the OP done so that would be reasonable. However,
it's pointless for a respondent to make that his only contribution to
ask. Especially when he full-quoted and was unable to read the original
post for context.
It's bad enough when we have people complaining about things
that are marginal (when in doubt, allow it), but to complain
about following the guidelines in the FAQ...

This is incorrect, as I didn't do that. You've made a strawman and
knocked it down. Congrats for going with the classics.
--
James Kanze (GABI Software, from CAI) email:[email protected]
Conseils en informatique orientie objet/
Beratung in objektorientierter Datenverarbeitung
9 place Simard, 78210 St.-Cyr-l'Icole, France, +33 (0)1 30 23 00 34

BTW, if you can't get Google to post this correctly, consider dropping
it. I understand that some people have no choice but to use their
inadequate system, but it's incumbent upon you to work with the
problems.




Brian
 
J

James Kanze

James Kanze wrote:

[...]
That's fine, and had the OP done so that would be reasonable.
However, it's pointless for a respondent to make that his only
contribution to ask. Especially when he full-quoted and was
unable to read the original post for context.

Just as pointless as for you to criticize the respondent for
doing so. His comment was technically correct. Your criticism
of it wasn't.
This is incorrect, as I didn't do that. You've made a strawman and
knocked it down. Congrats for going with the classics.
BTW, if you can't get Google to post this correctly, consider dropping
it. I understand that some people have no choice but to use their
inadequate system, but it's incumbent upon you to work with the
problems.

Modulo errors of manipulation on my part, my postings are
conform, even if they're not what I want. If OE can't quote
them correctly, it's not an acceptable newsreader. (Which
doesn't mean that I'll stop trying to get them to look like what
I want. I don't like quoted-ascii, and IMHO there's no need for
it today.)
 
D

Default User

James said:
James Kanze wrote:
[...]
That's fine, and had the OP done so that would be reasonable.
However, it's pointless for a respondent to make that his only
contribution to ask. Especially when he full-quoted and was
unable to read the original post for context.

Just as pointless as for you to criticize the respondent for
doing so. His comment was technically correct.

No, it wasn't. He completely missed the sections where the OP had
flagged the errors. At best, you could say it was half right.
Your criticism of it wasn't.

It certainly was.

Modulo errors of manipulation on my part, my postings are
conform, even if they're not what I want.

You're responsible for the broken software you use. If GG can't do
..sigs right, then don't use them.
If OE can't quote
them correctly, it's not an acceptable newsreader.

What does OE have to do with anything? I'm talking about your broken
..sig separator, not quoting.





Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top