search
Japanese Chinese Nederlands Espanol Italiano Deutsch Francais Twitter Rss Feeds
.NET Framework GroupsView
Deployment Server
.NET Distributed_Apps
.NET
.NET ADO.NET
.NET ASP.NET
.NET ASP.NET Security
.NET ASP.NET Webcontrols
.NET ASP.NET Web Services
.NET Clr
.NET Compact Framework
.NET Drawing
.NET Interop
.NET Micro Porting
.NET Performance
.NET Web Services
.NET Windows Forms
.NET Windows Forms Controls
.NET General
.NET Csharp
.NET Visual Basic
.NET Vc
.NET Security
.NET Xml
Scripting Jscript
Scripting Visual Basicscript
Scripting Wsh
Smartphone Developer
Visual Basic Com
Visual Basic Controls
Visual Basic Crystal
Visual Basic Database Ado
Visual Basic Syntax
Visual Basic Vista Compatibility
Visual Basic Winapi
Vc Atl
Vc Debugger
Vc Language
Vc Mfc
Vc Stl
Visio Developer Visual Basica
Vsnet Debugging
Windows Powershell
Windowsce Embedded Vc
Xml
Xsl

Group SummariesView
.NET Framework
Access
BizTalk
Certifications
CRM
DDK
Exchange Server
FoxPro
French
French .NET
Games
German
German .NET
Graphic Design
IIS
Internet
ISA Server
Italian
Italian .NET
Maps
MCIS
Miscellaneous
Mobile Application Development
Money
MSN
Networking
Office
Ops Mgr
Publisher
Security
SharePoint
Small Business
Spanish
Spanish .NET
SQL Server
Systems Management Server
Transaction Server
Virtual PC / Virtual Server
Visual Studio
Win32
Windows 2000
Windows 2003 Server
Windows 7
Windows Live
Windows Media
Windows Update
Windows Vista
Windows XP
 

View All Microsoft Vc Language Posts  Ask A New Question 

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) - aarthi2

Sunday, June 17, 2007 6: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;

}
reply
 

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) - David Lowndes

Sunday, June 17, 2007 6:17 AM

Try adding .c_str():

out_a << achar.c_str() << endl;

Dave
reply

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) - Alex Blekhman

Sunday, June 17, 2007 6:45 AM

You forgot #include <string>.

Alex
reply

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) - Tim Roberts

Tuesday, June 19, 2007 2:12 AM

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

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) - Alex Blekhman

Tuesday, June 19, 2007 5:05 AM

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
reply

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) - David Wilkinson

Tuesday, June 19, 2007 9:28 AM

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
reply

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) - Alex Blekhman

Tuesday, June 19, 2007 12:07 PM

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
reply

Previous Microsoft Vc Language conversation.