模板类的问题,高手留步,谢谢

pusshi 2009-01-06 11:49:20
以下我写的模板类,但编译不通过,谁知道错哪了,怎么改
//#ifndef BAG_H
//#define BAG_H

//背包类,由一个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 <class 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 <class T>
bool Bag<T> ::del_bag(T& res)
{
vector<T>::iterator its= _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(; its != _baosu.end(); its++)
{
if( its->first == res.first)
{
_baosu.erase(its);
_bao.erase(it);
return true;
}
}


}
return false;
}

/*void Bag::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);

}
}

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

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

}*/


//#endif
/*int main()
{
T st1("华山剑", 13, 50);
T st2("华山刀", 12, 45);
Bag s;
s.add_bag(st1);
s.add_bag(st1);
s.add_bag(st2);
s.del_bag(st1);
s.display_bag();
s.display_bag(true);//查看背包里的东西
s.display_bag(false);//卖背包里的东西
while(1);
return 0;
}*/

...全文
117 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pusshi 2009-01-06
  • 打赏
  • 举报
回复
dddddddddddddddddddddddddddd
pusshi 2009-01-06
  • 打赏
  • 举报
回复
Dev-C++的,
谁知道怎么回事.
OenAuth.Net 2009-01-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 anglecloudy 的回复:]
少了几个template <class T>,改了,编译通过,不过

T st1("华山剑", 13, 50); //是什么??


C/C++ code
//背包类,由一个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 <class T>
class Bag//背…
[/Quote]


LZ用的什么编译器?我在VC6.0 GCC上编译都通过了。
我在1楼的代码
waizqfor 2009-01-06
  • 打赏
  • 举报
回复
UP了
pusshi 2009-01-06
  • 打赏
  • 举报
回复
78 F:\msdosd\gs\未命名1.cpp expected `;' before "its"
85 F:\msdosd\gs\未命名1.cpp `its' undeclared (first use this function)
还是不对呀,还是出现以上错识
OenAuth.Net 2009-01-06
  • 打赏
  • 举报
回复
少了几个template<class T>,改了,编译通过,不过

T st1("华山剑", 13, 50); //是什么??


//背包类,由一个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 <class 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 <class T>
bool Bag<T> ::del_bag(T& res)
{
vector<T>::iterator its= _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(; its != _baosu.end(); its++)
{
if( its->first == res.first)
{
_baosu.erase(its);
_bao.erase(it);
return true;
}
}


}
return false;
}
template <class 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 <class T>
void Bag<T>::display_bag(bool shop)
{
map<string, int>::iterator it=_bao.begin();
vector<T>::iterator its=_baosu.begin();
if(shop)
{

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

}

int main()
{
/* T st1("华山剑", 13, 50); //是什么??
T st2("华山刀", 12, 45);
Bag s;
s.add_bag(st1);
s.add_bag(st1);
s.add_bag(st2);
s.del_bag(st1);
s.display_bag();
s.display_bag(true);//查看背包里的东西
s.display_bag(false);//卖背包里的东西
while(1);
*/
return 0;
}



hhyttppd 2009-01-06
  • 打赏
  • 举报
回复
未注释的部分?
xiaoyisnail 2009-01-06
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 pusshi 的回复:]
dddddddddddddddddddddddddddd
[/Quote]

dev-cpp里要加上typename显示告诉编译器后面的是类型


//背包类,由一个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 <class 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 <class T>
bool Bag<T> ::del_bag(T& res)
{
typename vector<T>::iterator its= _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(; its != _baosu.end(); its++)
{
if( its->first == res.first)
{
_baosu.erase(its);
_bao.erase(it);
return true;
}
}


}
return false;
}
template <class T>
void Bag<T>::add_bag(T res)
{
typename 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 <class T>
void Bag<T>::display_bag(bool shop)
{
typename map<string, int>::iterator it=_bao.begin();
typename vector<T>::iterator its=_baosu.begin();
if(shop)
{

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

}

int main()
{
/* T st1("华山剑", 13, 50); //是什么??
T st2("华山刀", 12, 45);
Bag s;
s.add_bag(st1);
s.add_bag(st1);
s.add_bag(st2);
s.del_bag(st1);
s.display_bag();
s.display_bag(true);//查看背包里的东西
s.display_bag(false);//卖背包里的东西
while(1);
*/
return 0;
}

65,211

社区成员

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

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