“默认删除”尝试将 unique_ptr 与向量一起使用以实现 C++ 中的多态性时出错;

发布于 2025-01-18 13:55:19 字数 3386 浏览 4 评论 0 原文

我对 C++ 比较陌生,我希望有人可以帮助我解决 unique_ptr 和向量遇到的问题。本质上,我试图使用多态性,以便我有一个“Base”类型的向量,它是一个抽象类(纯虚拟)。然后我尝试用派生类填充这个向量。我在下面提供了一个简单的示例,准确地展示了我想要实现的目标。请注意,我需要使用 C++11,这就是为什么我没有使用“std::make_unique”。 代码编译得很好,但我在类 Animal 中收到有关“default_delete”的运行时错误。

一个相关的问题是我应该使用 unique_ptrs 来实现运行时多态性,如下所示?或者我应该使用原始指针?

下面是头文件和CPP文件。 VS 的错误输出包含在下面。非常感谢您对此提供的任何帮助。

头文件:

#ifndef START_H
#define START_H
#include <vector>
#include <memory>



class Animal
{
public:
    virtual ~Animal() = default;
    void run();
    void setNumLegs(int a) { numLegs = a; }
    const int getLegs() const { return numLegs; }
private:
    double numLegs;
    virtual void useLegs() = 0;
};

class Biped : public Animal
{
private:
    void useLegs();
};

class Multiped : public Animal
{
public:
    double costOfShoes{ 12.0 };
private:
    void useLegs();
    void payForShoes();
    void becomeDestitute();
};

class Farm
{
public:
    std::vector<std::unique_ptr<Animal>> animals;
};


class Countryside
{
public:
    std::vector<std::unique_ptr<Farm>> farms;

};
#endif // START_H

CPP 文件:

#include "start.h"
#include <iostream>

int main() {
    
    Countryside countryside;

    std::unique_ptr<Farm> f(new Farm);

    std::vector<int> legs = { 2,4,5,2,10 };
    for (auto& numLegs : legs) {
        if (numLegs == 2) {
            std::unique_ptr<Biped> biped(new Biped);
            biped->setNumLegs(numLegs);
            f->animals.push_back(std::move(biped));
        }
        else if (numLegs > 2) {
            std::unique_ptr<Multiped> multiped(new Multiped);
            multiped->setNumLegs(numLegs);
            f->animals.push_back(std::move(multiped));
        }
    }
    
    countryside.farms.push_back(std::move(f)); //THIS IS WHERE THE PROBLEM IS I THINK

    for (auto& animal : f->animals) {
        animal-> run();
    }
    return 0;
}

void Animal::run() 
{
    useLegs();
}

void Biped::useLegs()
{
    std::cout << "Running with: "<< getLegs() <<"legs\n";
}

void Multiped::useLegs()
{
    std::cout << "Running with many legs:" << getLegs() << "!!! legs\n";
    payForShoes();

}
void Multiped::payForShoes() 
{
    std::cout << "Paying for shoes...\n";
    becomeDestitute();
}

void Multiped::becomeDestitute() 
{
    std::cout << "I have no money left.\n";
}

调试器错误输出:

  •  _Mypair <结构为 NULL> std::_Compressed_pa​​ir>>,std: :_Vector_val>>>,1>
    
  •  _Mypair._Myval2 <结构为 NULL> std::_Vector_val>>>>>
    
  •  这个 0x00000000  std::vector>,std::allocator>>>> *
    

I'm relatively new to C++, and I'm hoping someone can help me resolve an issue I'm having with unique_ptr and vectors. Essentially I'm trying to use polymorphism so that I have a vector of type "Base", which is an abstract class (pure virtual). I'm then attempting to fill this vector with derived classes. I've included a trivial example below, showcasing exactly what I'm trying to achieve. Please note that I need to use C++11, which is why I haven't made use of "std::make_unique".
The code compiles fine, but I get run-time errors about "default_delete" in class Animal.

A related question is should I be using unique_ptrs for run-time polymorphism as below? Or should I be using raw pointers instead?

Header file and CPP files below. Error output from VS is included below this. Very many thanks in advance for any help with this.

HEADER FILE:

#ifndef START_H
#define START_H
#include <vector>
#include <memory>



class Animal
{
public:
    virtual ~Animal() = default;
    void run();
    void setNumLegs(int a) { numLegs = a; }
    const int getLegs() const { return numLegs; }
private:
    double numLegs;
    virtual void useLegs() = 0;
};

class Biped : public Animal
{
private:
    void useLegs();
};

class Multiped : public Animal
{
public:
    double costOfShoes{ 12.0 };
private:
    void useLegs();
    void payForShoes();
    void becomeDestitute();
};

class Farm
{
public:
    std::vector<std::unique_ptr<Animal>> animals;
};


class Countryside
{
public:
    std::vector<std::unique_ptr<Farm>> farms;

};
#endif // START_H

CPP FILE:

#include "start.h"
#include <iostream>

int main() {
    
    Countryside countryside;

    std::unique_ptr<Farm> f(new Farm);

    std::vector<int> legs = { 2,4,5,2,10 };
    for (auto& numLegs : legs) {
        if (numLegs == 2) {
            std::unique_ptr<Biped> biped(new Biped);
            biped->setNumLegs(numLegs);
            f->animals.push_back(std::move(biped));
        }
        else if (numLegs > 2) {
            std::unique_ptr<Multiped> multiped(new Multiped);
            multiped->setNumLegs(numLegs);
            f->animals.push_back(std::move(multiped));
        }
    }
    
    countryside.farms.push_back(std::move(f)); //THIS IS WHERE THE PROBLEM IS I THINK

    for (auto& animal : f->animals) {
        animal-> run();
    }
    return 0;
}

void Animal::run() 
{
    useLegs();
}

void Biped::useLegs()
{
    std::cout << "Running with: "<< getLegs() <<"legs\n";
}

void Multiped::useLegs()
{
    std::cout << "Running with many legs:" << getLegs() << "!!! legs\n";
    payForShoes();

}
void Multiped::payForShoes() 
{
    std::cout << "Paying for shoes...\n";
    becomeDestitute();
}

void Multiped::becomeDestitute() 
{
    std::cout << "I have no money left.\n";
}

DEBUGGER ERROR OUTPUT:

  •   _Mypair <struct at NULL>    std::_Compressed_pair<std::allocator<std::unique_ptr<Animal,std::default_delete<Animal>>>,std::_Vector_val<std::_Simple_types<std::unique_ptr<Animal,std::default_delete<Animal>>>>,1>
    
  •   _Mypair._Myval2 <struct at NULL>    std::_Vector_val<std::_Simple_types<std::unique_ptr<Animal,std::default_delete<Animal>>>>
    
  •   this    0x00000000 <NULL>   std::vector<std::unique_ptr<Animal,std::default_delete<Animal>>,std::allocator<std::unique_ptr<Animal,std::default_delete<Animal>>>> *
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○闲身 2025-01-25 13:55:19

嗨,问题在于,您是在for循环中推迟一个空指针( f 设置为 nullptr )。
移动操作移动指向的项目的所有权,并将指针F设置为nullptr(移动语义)
移动后,无法再使用对象

Hi the problem is you are deferencing a null pointer in the for loop (f is set to nullptr).
The move operation moves the ownership of the item pointed to and the pointer f is set to nullptr (move semantics)
After a move the object can no longer be used

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文