std::string 在 push_back 到 std::vector 期间被删除的对象中
std::string in object being deleted during push_back to std::vector
我有一个 class,里面有一个 std::string。 class 本身完全没问题,但问题是我有另一个 class,其中包含第一个 std::vector。关键是,在第二个 class 的构造函数中,我从文件中加载了一些名称,我尝试 push_back 第一个 class 的对象到第二个中的向量。第一个 class 的对象构造完美(我尝试读取其中的 std::string )但是当它们被推到向量时,字符串就消失了!我尝试使用 emplace_back 但这也无济于事。这是代码:
#include <vector>
#include <iostream>
#include <fstream>
class CItemTemplate {
protected:
char id;
std::string name;
public:
CItemTemplate ():id(0),name(""){};
CItemTemplate (char _id, std::string _name) : id(_id){name = _name;};
CItemTemplate (const CItemTemplate& pattern) {id = pattern.GetID();}
~CItemTemplate (){};
char GetID() const {return id;};
std::string GetName() {return name;}
//sf::Texture* GetTexture() const {return tekstura;}
};
class CItemSystem {
std::vector<CItemTemplate> item_list;
public:
CItemSystem(std::string fpthItems);
~CItemSystem(){}
void list() {
for (unsigned int i = 0; i < item_list.size(); ++i) {
std::cout << (int)item_list[i].GetID() << " " << item_list[i].GetName() << std::endl << std::endl;
}
}
};
CItemSystem::CItemSystem(std::string fpthItems){
std::ifstream file;
file.open(fpthItems);
if (!file.is_open()) std::cout << "error!\n";
char name[64];
CItemTemplate tempitem;
item_list.push_back(CItemTemplate());
std::cout << "loading items...\n\n";
for (int i = 1;!file.eof();++i) {
file.getline(name,64,'|');
std::cout << CItemTemplate(i,(std::string)name).GetName() << std::endl << std::endl;
item_list.push_back(CItemTemplate(i,(std::string)name));
}
file.close();
}
int main()
{
CItemSystem ItemSystem("items.txt");
std::cout << "writing list of items...\n\n";
ItemSystem.list();
return 0;
}
};
CItemSystem::CItemSystem(std::string fpthItems){
std::ifstream file;
file.open(fpthItems);
if (!file.is_open()) std::cout << "error!\n";
char name[64];
CItemTemplate tempitem;
item_list.push_back(CItemTemplate());
std::cout << "loading items...\n\n";
for (int i = 1;!file.eof();++i) {
file.getline(name,64,'|');
std::cout << CItemTemplate(i,(std::string)name).GetName() << std::endl << std::endl;
item_list.push_back(CItemTemplate(i,(std::string)name));
}
file.close();
}
CItem CItemSystem::Item(char id) {
return (CItem(item_list[id]));
}
int main()
{
CItemSystem ItemSystem("items.txt");
CItem przedmiota = ItemSystem.Item(2);
CItem przedmiotb = ItemSystem.Item(4);
std::cout << "writing list of items...\n\n";
ItemSystem.list();
return 0;
}
结果:
loading items...
Bottle
Plug
Plugged bottle
Water
Unplugged bottle of water
Plug with water
Bottle of water
writing list of items...
0
1
2
3
4
5
6
7
8
przedmioty.txt 文件:
Bottle|Plug|Plugged bottle|Water|Plugged bottle of water|Plug with water|Bottle of water|
您的复制构造函数(采用 const CItemTemplate&
的构造函数)实现不正确:它不复制名称。
我有一个 class,里面有一个 std::string。 class 本身完全没问题,但问题是我有另一个 class,其中包含第一个 std::vector。关键是,在第二个 class 的构造函数中,我从文件中加载了一些名称,我尝试 push_back 第一个 class 的对象到第二个中的向量。第一个 class 的对象构造完美(我尝试读取其中的 std::string )但是当它们被推到向量时,字符串就消失了!我尝试使用 emplace_back 但这也无济于事。这是代码:
#include <vector>
#include <iostream>
#include <fstream>
class CItemTemplate {
protected:
char id;
std::string name;
public:
CItemTemplate ():id(0),name(""){};
CItemTemplate (char _id, std::string _name) : id(_id){name = _name;};
CItemTemplate (const CItemTemplate& pattern) {id = pattern.GetID();}
~CItemTemplate (){};
char GetID() const {return id;};
std::string GetName() {return name;}
//sf::Texture* GetTexture() const {return tekstura;}
};
class CItemSystem {
std::vector<CItemTemplate> item_list;
public:
CItemSystem(std::string fpthItems);
~CItemSystem(){}
void list() {
for (unsigned int i = 0; i < item_list.size(); ++i) {
std::cout << (int)item_list[i].GetID() << " " << item_list[i].GetName() << std::endl << std::endl;
}
}
};
CItemSystem::CItemSystem(std::string fpthItems){
std::ifstream file;
file.open(fpthItems);
if (!file.is_open()) std::cout << "error!\n";
char name[64];
CItemTemplate tempitem;
item_list.push_back(CItemTemplate());
std::cout << "loading items...\n\n";
for (int i = 1;!file.eof();++i) {
file.getline(name,64,'|');
std::cout << CItemTemplate(i,(std::string)name).GetName() << std::endl << std::endl;
item_list.push_back(CItemTemplate(i,(std::string)name));
}
file.close();
}
int main()
{
CItemSystem ItemSystem("items.txt");
std::cout << "writing list of items...\n\n";
ItemSystem.list();
return 0;
}
};
CItemSystem::CItemSystem(std::string fpthItems){
std::ifstream file;
file.open(fpthItems);
if (!file.is_open()) std::cout << "error!\n";
char name[64];
CItemTemplate tempitem;
item_list.push_back(CItemTemplate());
std::cout << "loading items...\n\n";
for (int i = 1;!file.eof();++i) {
file.getline(name,64,'|');
std::cout << CItemTemplate(i,(std::string)name).GetName() << std::endl << std::endl;
item_list.push_back(CItemTemplate(i,(std::string)name));
}
file.close();
}
CItem CItemSystem::Item(char id) {
return (CItem(item_list[id]));
}
int main()
{
CItemSystem ItemSystem("items.txt");
CItem przedmiota = ItemSystem.Item(2);
CItem przedmiotb = ItemSystem.Item(4);
std::cout << "writing list of items...\n\n";
ItemSystem.list();
return 0;
}
结果:
loading items...
Bottle
Plug
Plugged bottle
Water
Unplugged bottle of water
Plug with water
Bottle of water
writing list of items...
0
1
2
3
4
5
6
7
8
przedmioty.txt 文件:
Bottle|Plug|Plugged bottle|Water|Plugged bottle of water|Plug with water|Bottle of water|
您的复制构造函数(采用 const CItemTemplate&
的构造函数)实现不正确:它不复制名称。