程序有bug,望指点....

redman27333 2011-10-17 04:53:05
 #include <iostream> 
#include<string>
#include<vector>
using namespace std;
class Assoc{
struct Pair{
string name;
double val;
Pair(string n="",double v=0):name(n),val(v){}
};
vector<Pair> vec;
Assoc (const Assoc &);
Assoc &operator =(const Assoc&);
public:
Assoc(){}
const double & operator []( const string &);
double & operator [](string &);
void print_all() const;
};
const double & Assoc::operator[]( const string &s)
{
for(vector<Pair>::const_iterator p=vec.begin();p!=vec.end();++p)
if(s==p->name) return p->val;
vec.push_back(Pair(s,0));
return vec.back().val;
}

void Assoc::print_all() const
{ for(vector <Pair>::const_iterator p= vec.begin();p!=vec.end();p++)
cout<<p->name<<" : "<<p->val<<endl;
}
int main()
{ string buf;
Assoc vec;
while(cin>>buf) vec[buf]++ ;;
vec.print_all();
return 0;
}
...全文
107 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengmingtao 2011-10-17
  • 打赏
  • 举报
回复
你在类里生命了两个operator[],但是只定义了一个。
const double & operator []( const string &);
double & operator [](string &);

另外,
vec[buf]++ ;;是想增加pair::value吗?但是pair是private的,main函数不能直接访问,哪怕是用返回值~。

重新设计吧
blingpro 2011-10-17
  • 打赏
  • 举报
回复
问题?什么bug

65,210

社区成员

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

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