这程序提示:error C2678: 二进制“>>”: 没有找到接受“std::istream”类型的左操作数的运算符(或没有可接受的转换)是什么问题,VS编译

SenySunny 2012-11-09 03:52:54
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <fstream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
ofstream a;
string b;
cout << "请输入文件名称:";
cin >> b;
b += ".txt";
a.open(b);
a << "hello!" << endl;
a << 22.35 << endl;
a.close(b);

return 0;
}


显示cin >> b;错误,原因是:error C2678: 二进制“>>”: 没有找到接受“std::istream”类型的左操作数的运算符(或没有可接受的转换) 求高手解释!!!,本人用VS2010编译!!!
...全文
3062 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jshzncd 2014-09-06
  • 打赏
  • 举报
回复
我也遇到了楼主的问题,后来只需要在头文件加上#include <string> 就好了
SenySunny 2012-11-09
  • 打赏
  • 举报
回复
引用 9 楼 a42795761 的回复:
我用vc6.0运行了一下,全是错误, 首先头文件,需要包含#include<string>而不是cstring。 然后是ofstream的open函数,你还是再看下参数吧,里面传的参数貌似是const char *以及一个读写方式 最后,ofstream的closr(),只需要直接close就行了没必要再把那个b传进去你修改下再试试 #include "stdaf……
是的!谢谢!!的确是这样!!!呵呵!!刚刚学C++!初学者!!
请叫我杨二哥 2012-11-09
  • 打赏
  • 举报
回复
我用vc6.0运行了一下,全是错误,
首先头文件,需要包含#include<string>而不是cstring。
然后是ofstream的open函数,你还是再看下参数吧,里面传的参数貌似是const char *以及一个读写方式
最后,ofstream的closr(),只需要直接close就行了没必要再把那个b传进去你修改下再试试
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;


int main()
{
ofstream a;
string b;
cout << "请输入文件名称:";
cin >> b;
b += ".txt";
a.open(b.c_str());
a << "hello!" << endl;
a << 22.35 << endl;
a.close();

return 0;
}

//我修改的 你看下对不对
SenySunny 2012-11-09
  • 打赏
  • 举报
回复
引用 7 楼 zjs100901 的回复:
不对,应该说: 没有扩展名的头文件,类型名在名字空间std里。 有扩展名的头文件,类型名没有加入到名字空间中。
也就是说我们要用string类的话,必须添加#include<string>,而cstring中包含的仅仅是字符串的处理函数??其实这个程序完全没有必要包含#include<cstring>,测试了一下,应该是这样的!谢谢啊!
zjs100901 2012-11-09
  • 打赏
  • 举报
回复
不对,应该说: 没有扩展名的头文件,类型名在名字空间std里。 有扩展名的头文件,类型名没有加入到名字空间中。
zjs100901 2012-11-09
  • 打赏
  • 举报
回复
cstring里面包含的是string.h STL里面的那些东东没有扩展名的说。
SenySunny 2012-11-09
  • 打赏
  • 举报
回复
引用 4 楼 zjs100901 的回复:
C/C++ code12345678910111213141516171819202122#include "stdafx.h"#include <iostream>#include <cstring>#include <fstream>#include <string> using namespace std; int _tmain(int argc, _TCHAR*……
能解释下为什么吗?貌似在cstring 里面有包含string这个文件啊!
zjs100901 2012-11-09
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
 
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    ofstream a;
    string b;
    cout << "请输入文件名称:";
    cin >> b;
    b += ".txt";
	a.open(b.c_str());
    a << "hello!" << endl;
    a << 22.35 << endl;
    a.close();
 
    return 0;
}
zjs100901 2012-11-09
  • 打赏
  • 举报
回复
#include <string>
「已注销」 2012-11-09
  • 打赏
  • 举报
回复
你这样子就好像 printf("%s",CString("1111111111111111")); 人家不识别
「已注销」 2012-11-09
  • 打赏
  • 举报
回复
char arr[256]; cin>>arr; b = arr; 加上去就好了

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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