求问我的程序抛出std::bad_alloc异常的问题```
前几天不能上csdn,又正好在学C++``````真是快憋出病来了,哭```
偶初学的C++,本来是用java的,所以很多地方不太能理解```这两天用C++搞一个小程序,遇到std::bad_alloc异常,现在还不理解到底是为什么出错,要怎么处理,请高人指点了。
下面是我的程序,三个文件,用的vs2008编译,工程是一个控制台程序,没用.net类库,就用了stl
文件一:
/*! @file
********************************************************************************
<PRE>
文件名 : tag.h
相关文件 : tag.cpp
文件实现功能 : 表示一个xml标签的类tag的声明(tag)
一个包函两个字符串的结构体SKey(xml标签的基础,key为标签,value为内容)
*******************************************************************************/
#include <string>
#include <list>
namespace victim{
struct SKey{
std::string key;
std::string value;
};
class Tag{
private:
SKey base;//标签
std::list<SKey> propertyList;//属性列表
std::list<Tag*> subTagList;//子元素列表
public:
//初使化
Tag(std::string);
Tag(std::string,std::string);
//get set
std::string setKey(std::string);
std::string setValue(std::string);
std::list<Tag*> setSubTagList(Tag);
std::list<SKey> setPropertyList(SKey);
std::string getKey();
std::string getValue();
std::list<Tag*> getSubTagList();
std::list<SKey> getPropertyList();
};
}
文件二:
/*! @file
********************************************************************************
<PRE>
文件名 : tag.cpp
相关文件 : tag.h
文件实现功能 : 表示一个xml标签的类tag的实现
*******************************************************************************/
#include "tag.h"
using namespace std;
//victim.tag init
victim::Tag::Tag(string key,string value){
setKey(key);
setValue(value);
}
victim::Tag::Tag(string key){
setKey(key);
setValue("");
}
//victim.tag get and set
string victim::Tag::setKey(string key){
base.key=key;
return base.key;
}
string victim::Tag::setValue(string value){
base.value=value;
return base.value;
}
std::list<victim::Tag *> victim::Tag::setSubTagList(victim::Tag subTag){
subTagList.push_back(&subTag);
return subTagList;
}
std::list<victim::SKey> victim::Tag::setPropertyList(victim::SKey skey){
propertyList.push_back(skey);
return propertyList;
}
std::string victim::Tag::getKey(){
return base.key;
}
std::string victim::Tag::getValue(){
return base.value;
}
std::list<victim::Tag *> victim::Tag::getSubTagList(){
return subTagList;
}
std::list<victim::SKey> victim::Tag::getPropertyList(){
return propertyList;
}
文件三:(这里是main)
#include <string>
#include <iostream>
#include "tag.h"
using namespace std;
using namespace victim;
void main(){
Tag* my = new Tag("my");
Tag* name = new Tag("name","victim");
Tag* age = new Tag("age","24");
Tag* sex = new Tag("sex","男");
(*my).setSubTagList(*my);
(*my).setSubTagList(*age);
(*my).setSubTagList(*sex);
cout<<my->getKey()<<endl;
list<Tag*> subTagList = my->getSubTagList();
for(list<Tag*>::iterator iter=subTagList.begin();iter!=subTagList.end();++iter){
Tag *tag = *iter;
cout<<tag->getKey()<<":"<<tag->getValue()<<endl;
//就是这句会抛的异常,如果注释掉就没事,只要一使用这个引用就出异常了,但是我不明白这句哪里new了新的对象
}
system("PAUSE");
}
麻烦高手帮忙解释一下了```谢谢大家````
另外希望认识一些C++的朋友,呵呵,为了方便学习```大家方便的话随个QQ或MSN,偶加你们,感激不尽````
[解决办法]
不好意思,前面我看错了。
std::list <victim::Tag *> victim::Tag::setSubTagList(victim::Tag subTag)
中subTag是一个临时变量,在函数结束之后所占用的内存单元就释放了,但你引用了它的地址,从而造成了错误。改为
std::list <victim::Tag *> victim::Tag::setSubTagList(victim::Tag *subTag)
还有就是main函数中new的对象没有delete
[解决办法]
[code=C/C++][/code]//get set
std::string setKey(std::string);
std::string setValue(std::string);
//std::list <Tag*> setSubTagList(Tag);
std::list <Tag*> setSubTagList(Tag&);//这里应该传递引用,因为你push_back的是指针
std::list <SKey> setPropertyList(SKey);