自己写的游戏容器模板,不能通过编译,请高手指教一下

pusshi 2009-05-04 07:52:20
以下是一个角色背包容器模板,不知道为什么通不过编译,高手指教.出现以下错误
F:\msdosd\gs\未命名1.cpp In member function `bool Bag<T>::del_bag(T&)':
F:\msdosd\gs\未命名1.cpp In member function `bool Bag<T>::del_bag(T&)':
F:\msdosd\gs\未命名1.cpp In member function `bool Bag<T>::del_bag(T&)':
F:\msdosd\gs\未命名1.cpp In member function `bool Bag<T>::del_bag(T&)':
F:\msdosd\gs\未命名1.cpp In member function `bool Bag<T>::del_bag(T&)':
F:\msdosd\gs\未命名1.cpp In member function `bool Bag<T>::del_bag(T&)':
F:\msdosd\gs\未命名1.cpp In member function `bool Bag<T>::del_bag(T&)':
//背包类,由一个map记录武器名字和个数,vector记录武器属性;
#include <iostream>
#include <string>
#include <vector>
#include <map>
//#include "F:\msdosd\gs\head\key.h"
using namespace std;
//typedef vector<Key> mups;
//typedef map<string,int> mup;
template <typename T>
class Bag//背包类
{
public:
Bag(){ }

bool del_bag(T& res);

void add_bag(T res);

bool emptybag(){return _bao.empty();}

void display_bag(bool shop=true);

vector<T> _baosu;//武器属性;
map<string, int> _bao ;//武器名字和个数


};

template <typename T>
bool Bag<T> ::del_bag(T& res)
{
vector<T>::iterator itis;
itis= _baosu.begin();
map<string, int>::iterator it;
it=_bao.find(res.first);
if( (it != _bao.end()) && (it->second >1) )
{
it->second--;
return true;
}

if( (it != _bao.end()) && (it->second =1) )
{
for(; itis != _baosu.end(); itis++)
{
if( itis->first == res.first)
{
_baosu.erase(itis);
_bao.erase(it);
return true;
}
}


}
return false;
}
template <typename T>
void Bag<T>::add_bag(T res)
{
map<string, int>::iterator it=_bao.begin();
it=_bao.find(res.first);
if(it != _bao.end())
{
it->second++;
}
else
{
_bao.insert(make_pair(res.first, 1));
_baosu.push_back(res);

}
}
template <typename T>
void Bag<T>::display_bag(bool shop)
{
map<string, int>::iterator it=_bao.begin();
vector<T>::iterator itis=_baosu.begin();
if(shop)
{

for(; it != _bao.end(); ++it)
{
cout<<it->first<<"\t数量:"<<it->second;
for(itis=_baosu.begin(); itis != _baosu.end();itis++)
{
if(itis->first == it->first)
{
cout<<"\t攻:"<<itis->second<<"\t";
}
}
}
cout<<endl;
}
else
{
for(; it != _bao.end(); ++it)
{
cout<<it->first<<"\t数量:"<<it->second;
for(itis=_baosu.begin(); itis != _baosu.end();itis++)
{
if(itis->first == it->first)
{
cout<<"\t攻:"<<itis->second<<"\t钱:"<<(it->second)*(itis->money)<<"\t";
}
}
}
cout<<endl;
}

}

int main()
{

return 0;
}
...全文
116 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
adventurelw 2009-05-04
  • 打赏
  • 举报
回复
我就一个地方看不懂
if(itis->first == it->first)

itis不是vector迭代器么,如何有的first成员?
难道vector元素是pair对象?
mengde007 2009-05-04
  • 打赏
  • 举报
回复
实在找不出错误来;
lingyin55 2009-05-04
  • 打赏
  • 举报
回复
你把typename 都换成class试试

[Quote=引用 7 楼 pusshi 的回复:]
请高手指教一下呀,
[/Quote]
pusshi 2009-05-04
  • 打赏
  • 举报
回复
请高手指教一下呀,
pusshi 2009-05-04
  • 打赏
  • 举报
回复
在VC6++里是没问题的,为什么在DEV-C++4.9里有以上问题
qqwx_1986 2009-05-04
  • 打赏
  • 举报
回复
楼主什么编译器,你说的编译错误没有
  • 打赏
  • 举报
回复
你什么编译器啊
lingyin55 2009-05-04
  • 打赏
  • 举报
回复
vs2008编译通过

1>Compiling...
1>try.cpp
1>Compiling manifest to resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Linking...
1>Embedding manifest...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Build log was saved at "file://e:\vtech\split\Debug\BuildLog.htm"
1>split - 0 error(s), 0 warning(s)
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

运行也没问题

请按任意键继续. . .
pusshi 2009-05-04
  • 打赏
  • 举报
回复
不好意思,错误信息好像没复制上
F:\msdosd\gs\未命名1.cpp expected `;' before "itis"
34 F:\msdosd\gs\未命名1.cpp `itis' undeclared (first use this function)
86 F:\msdosd\gs\未命名1.cpp `itis' undeclared (first use this function)
liliangbao 2009-05-04
  • 打赏
  • 举报
回复
帮顶~

65,211

社区成员

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

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