std::make_pair自定义类的分段错误

std::make_pair segmentation fault with custom class

本文关键字:分段 错误 pair make std 自定义      更新时间:2023-10-16

我已经调整了一个自定义包装器以包含不同类型的数据类型,但是当我进行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=