Я получаю ошибку сегментации, когда я пытаюсь вставить ее в свою карту.
Функция выглядит примерно так:
void add(std::string id, std::string name)
{
Asset asset(nullptr, false, name);
mAssets.insert(std::make_pair<std::string, Asset>(id,asset)); <-- This line gives segfault
}
mAssets просто объявляется
std::map<assetID, Asset> mAssets;
И класс Asset (sloppy) объявлен следующим образом:
class Asset
{
public:
Asset(T* a, bool l, std::string f) : asset(a), loaded(l), filename(f)
{
}
Asset(const Asset& copy)
{
loaded = copy.loaded;
filename = copy.filename;
asset = new T();
*asset = *copy.asset;
}
~Asset()
{
delete asset;
}
Asset& operator=(const Asset& other)
{
Asset temp(other);
loaded = temp.loaded;
filename = temp.filename;
std::swap(asset,temp.asset);
return *this;
}
T* asset;
bool loaded;
std::string filename;
};
Ваша проблема здесь в вашем конструкторе копирования:
asset = new T();
*asset = *copy.asset;
Я оставлю это вам, чтобы понять, почему...
В вашем конструкторе копирования вы разыгрываете нулевой указатель:
*asset = *copy.asset
из
Asset asset(nullptr, false, name);
Проверьте свои указатели и избегайте разыменования нулевых указателей:
Asset(const Asset& copy)
{
loaded = copy.loaded;
filename = copy.filename;
if (copy.asset)
{
asset = new T(); // better may be asset = new T(copy)
*asset = *copy.asset;
}
else
{
asset = nullptr
}
}
* asset = * copy.asset; // вы должны проверить, является ли актив NULL или нет, тогда проверьте на наличие актива
Так будет работать ваш код:
#include <iostream>
#include <map>
using namespace std;
template<class T>
class Asset
{
public:
Asset(T* a, bool l, std::string f) : asset(a), loaded(l), filename(f)
{
}
Asset(const Asset& copy)
{
cout<<"copy"<<endl;
loaded = copy.loaded;
filename = copy.filename;
asset = new T();
if(© != NULL)
{
if(copy.asset != NULL)
*asset = *(copy.asset);
}
}
~Asset()
{
delete asset;
}
Asset& operator=(const Asset& other)
{
Asset temp(other);
loaded = temp.loaded;
filename = temp.filename;
std::swap(asset,temp.asset);
return *this;
}
T* asset;
bool loaded;
std::string filename;
};
std::map <string,Asset<int> > mAssets;
void add(std::string id, std::string name)
{
Asset<int> asset(NULL, false, name);
mAssets.insert(std::make_pair<std::string, Asset<int> >(id,asset)); //<-- This line gives segfault
}
int main()
{
add("1","hi");
cout<<"run"<<endl;
}
if(© != NULL)