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

Discussion in 'C++' started by aarthi28@gmail.com, Jun 17, 2007.

  1. Guest

    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;
    }
    , Jun 17, 2007
    #1
    1. Advertising

  2. Baltimore Guest

    On 17 juin, 11:52, 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;
    >
    > 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 ?
    Baltimore, Jun 17, 2007
    #2
    1. Advertising

  3. Ian Collins Guest

    Re: error C2679: binary '<<' : no operator found which takes a right-hand

    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.


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

    --
    Ian Collins.
    Ian Collins, Jun 17, 2007
    #3
  4. Ian Collins Guest

    Re: error C2679: binary '<<' : no operator found which takes a right-hand

    Baltimore wrote:
    > On 17 juin, 11:52, wrote:
    >> 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 ?
    >

    Did you have to quote the entire post to ask that?

    --
    Ian Collins.
    Ian Collins, Jun 17, 2007
    #4
  5. Guest

    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;
    ..
    ..
    ..
    , Jun 17, 2007
    #5
  6. Guest

    Is there something else I can do to make this run? I don't know why I
    am getting an error
    , Jun 17, 2007
    #6
  7. On Sun, 17 Jun 2007 09:52:06 -0000, 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.
    >Thank you
    >
    >#include <iostream>
    >#include <fstream>
    >#include <iterator>
    >#include <algorithm>
    >#include <vector>
    >#include <math.h>
    >#include "string.h"


    #include <string>


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
    Roland Pibinger, Jun 17, 2007
    #7
  8. Bo Persson Guest

    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
    Bo Persson, Jun 17, 2007
    #8
  9. Re: error C2679: binary '<<' : no operator found which takes a right-hand

    On 2007-06-17 11:52, 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"


    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.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jun 17, 2007
    #9
  10. Re: error C2679: binary '<<' : no operator found which takes a right-hand

    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


    You are forgetting

    #include <string>

    No .h and <> not ""

    john
    John Harrison, Jun 17, 2007
    #10
  11. James Kanze Guest

    On Jun 17, 11:52 am, 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::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.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 17, 2007
    #11
  12. Default User Guest

    wrote:

    > 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
    Default User, Jun 17, 2007
    #12
  13. Default User Guest

    Baltimore wrote:

    > On 17 juin, 11:52, 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)


    > > out_a << achar << endl; //ERROR
    > > out_n << n_word_list.at(i) << endl; //ERROR



    >
    > 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
    Default User, Jun 17, 2007
    #13
  14. James Kanze Guest

    On Jun 17, 8:05 pm, "Default User" <> wrote:
    > Baltimore wrote:
    > > On 17 juin, 11:52, 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)
    > > > out_a << achar << endl; //ERROR
    > > > out_n << n_word_list.at(i) << endl; //ERROR


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


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

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 17, 2007
    #14
  15. Default User Guest

    James Kanze wrote:

    > On Jun 17, 8:05 pm, "Default User" <> wrote:
    > > Baltimore wrote:
    > > > On 17 juin, 11:52, 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)
    > > > > out_a << achar << endl; //ERROR
    > > > > out_n << n_word_list.at(i) << endl; //ERROR

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

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

    >
    > 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
    Default User, Jun 18, 2007
    #15
  16. James Kanze Guest

    On Jun 18, 1:01 am, "Default User" <> wrote:
    > James Kanze wrote:
    > > On Jun 17, 8:05 pm, "Default User" <> wrote:
    > > > Baltimore wrote:
    > > > > On 17 juin, 11:52, 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)
    > > > > > out_a << achar << endl; //ERROR
    > > > > > out_n << n_word_list.at(i) << endl; //ERROR


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


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


    > > Since when? Questions concerning the conformance of a compiler
    > > are on topic.


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

    --
    James Kanze (GABI Software, from CAI) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 18, 2007
    #16
  17. Baltimore Guest

    On Jun 17, 12:10 pm, wrote:
    > 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++.
    Baltimore, Jun 18, 2007
    #17
  18. Default User Guest

    James Kanze wrote:

    > On Jun 18, 1:01 am, "Default User" <> wrote:
    > > James Kanze wrote:


    > > > Since when? Questions concerning the conformance of a compiler
    > > > are on topic.

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


    That wasn't the question, was it?

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


    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:
    > 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
    Default User, Jun 18, 2007
    #18
  19. James Kanze Guest

    On Jun 18, 8:18 pm, "Default User" <> wrote:
    > 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.

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


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

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 18, 2007
    #19
  20. Default User Guest

    James Kanze wrote:

    > On Jun 18, 8:18 pm, "Default User" <> wrote:
    > > 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.


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


    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
    Default User, Jun 18, 2007
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kim JM
    Replies:
    1
    Views:
    1,698
    Riyaz
    Apr 2, 2004
  2. c++newbie
    Replies:
    7
    Views:
    18,107
    trav580
    Feb 12, 2008
  3. Martin T.
    Replies:
    7
    Views:
    816
    Martin T.
    Mar 10, 2008
  4. Tassador

    const string error compilation - error C2679

    Tassador, Jul 19, 2009, in forum: C Programming
    Replies:
    0
    Views:
    623
    Tassador
    Jul 19, 2009
  5. s88
    Replies:
    3
    Views:
    1,053
    Jim Langston
    Sep 23, 2010
Loading...

Share This Page