返回信息流error C2679: binary '[' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
编一个小程序,就这个问题解决不了,上网查资料都是“<<”、“=”等符号的解决办法,没有“[”的,不知道应该怎么解决啊?
有人说“<<”什么的加<string>就能解决了,是不是“[”也要加什么头文件呢?
全部代码如下:
#include <map>
#include <string>
#include <iostream>
#include <fstream>
#include <strstream>
using namespace std;
void main(void)
{
map<string,int>mapLine;
int i=0;
char *rFileName="data.txt";
ifstream sfile(rFileName);
if(!sfile)
{
cerr<<rFileName<<"can't open!"<<endl;
exit(0);
}
char buf[100];
while(sfile.getline(buf,100))
{
sfile>>buf;
mapLine.insert(pair<string,int>(buf,i+1));
i++;
}
map<string,int>::iterator iter;
for(iter=mapLine.begin();iter!=mapLine.end();iter++)
cout<<iter->first<<" "<<iter->second<<endl;
/* int nSize=mapLine.size();
for(int nIndex=0;nIndex<nSize;nIndex++)
cout<<mapLine[nIndex]<<endl;
*/}
问题就在最后的nIndex索引,现在的程序已经能运行了,但是应用/**/中的就会报这个错
这是一条镜像帖。来源:北邮人论坛 / cpp / #48449同步于 2010/12/28
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
就没人遇到error C2679:binary '['么?
vanbio
2010/12/28镜像同步8 回复
订阅后,新回复会通过你的通知中心匿名送达。
8 条回复
【 在 vanbio 的大作中提到: 】
: error C2679: binary '[' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
: 编一个小程序,就这个问题解决不了,上网查资料都是“<<”、“=”等符号的解决办法,没有“[”的,不知道应该怎么解决啊?
: 有人说“<<”什么的加<string>就能解决了,是不是“[”也要加什么头文件呢?
: ...................
贴错误附近的代码...
已贴
【 在 shenlei 的大作中提到: 】
: : error C2679: binary '[' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
: : 编一个小程序,就这个问题解决不了,上网查资料都是“<<”、“=”等符号的解决办法,没有“[”的,不知道应该怎么解决啊?
: : 有人说“<<”什么的加<string>就能解决了,是不是“[”也要加什么头文件呢?
: ...................
我是按教材上的代码写的,其中的一种就能用,而遍历就不能用
代码已经贴上了
【 在 wo 的大作中提到: 】
: 你是不是用了内部结构是非数组的stl容器?,比如queue、list是没有operator[]运算符的,遍历这些容器需要用迭代器
: --
问题很简单...
map的第一个值是key,所以你使用mapLine索引应该使用string类型...
所以应该反过来写...map<int,string>...
mapLine.insert(pair<string,int>(buf,i+1));
这句可以看出来键值从1开始,最后那个循环也要从1开始查找键值...
【 在 vanbio (翛然) 的大作中提到: 】
: error C2679: binary '[' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
: 编一个小程序,就这个问题解决不了,上网查资料都是“<<”、“=”等符号的解决办法,没有“[”的,不知道应该怎么解决啊?
: 有人说“<<”什么的加<string>就能解决了,是不是“[”也要加什么头文件呢?
: ...................