Re: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
On Jun 17, 11:52 am, aarth...@gmail.com wrote:
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::ostream_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.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34