std::make_pair自定义类的分段错误
std::make_pair segmentation fault with custom class
我已经调整了一个自定义包装器以包含不同类型的数据类型,但是当我进行std::make_pair(customClass,customClass)
时它崩溃了,我已经使用 gdb 进行了调试,但我没有看到问题,此外我声明了这个变量并测试了它是否创建并且它有值。我已经深入研究了std::make_pair()
函数以及它的作用,它只构造了 std::p air 对象,但我的类是声明的,而不是指针。我没有看到问题...这是代码
#include <iostream>
#include <string>
#include <vector>
#include <memory>
class any_type
{
public:
virtual ~any_type() {}
virtual void print() = 0;
};
template <class T>
class concrete_type : public any_type
{
public:
concrete_type(const T& value) : value_(value)
{}
virtual void print()
{
std::cout << value_ << 'n';
}
T & get()
{
return dynamic_cast<concrete_type<T>&>(*this).value_;
}
//recently added
concrete_type(const concrete_type<T>& other) : value_(other.value_)
{}
T value_;
private:
};
class WrapperMultiContainner
{
public:
WrapperMultiContainner():mAnyType(0)
{
mAnyType=new concrete_type<int>(-1);
}
//recently added
WrapperMultiContainner(const WrapperMultiContainner & aCopy)
{
//recently added
mAnyType=new concrete_type<decltype(*(aCopy.mAnyType))>(*(aCopy.mAnyType));
//*mAnyType=aCopy.mAnyType;
}
const WrapperMultiContainner & operator=(const WrapperMultiContainner &)
{ return *this;}
template<typename T>
WrapperMultiContainner(T const & aValue= T()):mAnyType(0)
{
mAnyType=new concrete_type<T>(aValue);
}
~WrapperMultiContainner()
{
delete mAnyType;
}
template<typename T>
T & get()
{
return dynamic_cast<concrete_type<T>&>(*mAnyType).value_;
}
template<typename T>
void get(T & aValue,
int & aErrorCode)
{
try{
aValue=dynamic_cast<concrete_type<T>&>(*mAnyType).value_;
aErrorCode=0;
}
catch(...)
{
aErrorCode=-1;
}
//return dynamic_cast<concrete_type<T>&>(*mAnyType).value_;
}
any_type * getAnyType() const
{
return mAnyType;
}
template<typename T>
void set(T const & aGenericValue = T())
{
if(mAnyType)
{
delete mAnyType;
mAnyType=0;
}
mAnyType=new concrete_type<T>(aGenericValue);
}
private:
any_type * mAnyType;
};
int main()
{
std::cout<<"creando el opciones para el builder de comandos"<<std::endl;
//Creacion de las estructuras que tienen las opciones para la creacion de los comandos
std::string aKeyName("idcompdestiny");
WrapperMultiContainner aKey(aKeyName);
aKey.getAnyType()->print();
WrapperMultiContainner aValue(3000);
aValue.getAnyType()->print();
std::pair<WrapperMultiContainner,WrapperMultiContainner> aPair;
aPair=std::make_pair(aKey,aValue);
return 0;
}
创建 std::make_pair崩溃的行。谢谢你前进!
PD:我已经添加了复制构造函数,但仍然崩溃
从部分读取代码 - 您缺少WrapperMultiContainer
中的复制构造函数
看,如果不编写复制构造函数,将使用默认值。这将复制指针。由于现在两个类将具有相同的指针,并且都将在析构函数上删除它 - 您会遇到分段错误。
但同样,只读取了一半的代码。
编辑:出于同样的原因也做一个operator=
。
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?