为啥string不支持cout的<<操作?忧闷,还请高手指教!

softworms 2004-07-08 10:45:33
string str="bobo";
cout<<str;
系统提示:
D:\myproject\enum\enum.cpp(54) : error C2678: binary '<<' : no operator defined which takes a left-hand operand of type 'class ostream_withassign' (or there is no acceptable conversion)
Error executing cl.exe.
么回事呢?
...全文
100 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sohou 2004-07-09
  • 打赏
  • 举报
回复
你要加STL的头文件的啦
#include <iostream>
#include <string>
Creamdog 2004-07-09
  • 打赏
  • 举报
回复
getline出错?

Standard C++ Library Reference

getlineSee Also
<string> Members | string::getline Sample
Extract strings from the input stream line-by-line.

template<class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& _Str
);

template<class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& _Str,
CharType _Delim
);
Parameter
_Istr
The input stream from which a string is to be extracted.
_Str
The string into which are read the characters from the input stream.
_Delim
The line delimiter.
Return Value
The first function returns getline( _Istr, _Str, _Istr.widen( '\n' ) ).

The second function replaces the sequence controlled by _Str with a sequence of elements extracted from the stream _Istr.

Remarks
In order of testing, extraction stops:

At end of file.
After the function extracts an element that compares equal to delim, in which case the element is neither put back nor appended to the controlled sequence.
After the function extracts str.max_size elements, in which case the function calls setstate(ios_base::failbit).
If the function extracts no elements, it calls setstate(failbit). In any case, it returns _Istr.

See Also
<string> Members | string::getline Sample



--------------------------------------------------------------------------------

Send feedback on this topic to Microsoft

© 1992-2002 by P. J. Plauger. All rights reserved.

© Microsoft Corporation. All rights reserved.
softworms 2004-07-09
  • 打赏
  • 举报
回复
怪事,我家的机器不行啊?
难道是我装的VC有问题??
但确实是有错语提示的啊.忧闷.
Creamdog 2004-07-09
  • 打赏
  • 举报
回复
以下代码将可以将控制台中的一句话输入到string中

string x;
getline( cin, x );
alan001 2004-07-09
  • 打赏
  • 举报
回复
up
o1n 2004-07-09
  • 打赏
  • 举报
回复
支持呀

#include <iostream>
#include <string>
using namespace std;
void main()
{
string s="Do you understand??";
cout << s ;

}
dada9527 2004-07-09
  • 打赏
  • 举报
回复
basic_ostream& operator<<(basic_ostream& (*pf)(basic_ostream&));
basic_ostream& operator<<(basic_ios<E, T>& (*pf)(basic_ios<E, T>&));
basic_ostream& operator<<(ios_base<E, T>& (*pf)(ios_base<E, T>&));
basic_ostream& operator<<(basic_streambuf<E, T> *sb);
basic_ostream& operator<<(const char *s);
basic_ostream& operator<<(char c);
basic_ostream& operator<<(bool n);
basic_ostream& operator<<(short n);
basic_ostream& operator<<(unsigned short n);
basic_ostream& operator<<(int n);
basic_ostream& operator<<(unsigned int n);
basic_ostream& operator<<(long n);
basic_ostream& operator<<(unsigned long n);
basic_ostream& operator<<(float n);
basic_ostream& operator<<(double n);
basic_ostream& operator<<(long double n);
basic_ostream& operator<<(void * n);
//--------------------------------------------------------
怎么会不支持??
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string s("hehe");
cout << s << endl;
return 0;
}

Kudeet 2004-07-08
  • 打赏
  • 举报
回复
应该是cout的《不支持string类型吧

string str="bobo";
char *p = (char*)str.c_str();//转换一下类型

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧