【问题标题】:how to change the visibility of a vector of unique pointers如何更改唯一指针向量的可见性
【发布时间】:2020-07-05 09:07:35
【问题描述】:

在 ONG 类中,我创建了一个添加函数,将参与者(主管、管理员、员工)添加到参与者向量中。

std::vector<unique_ptr<Participant>> ls;

我试图将向量作为公共变量置于功能之外,但它不起作用

当我想在函数内部添加时,一切正常, 但是当我将列表置于功能之外时,它会给我一个错误

class ONG : public Participant {
private:
    
public:
    std::vector<unique_ptr<Participant>> ls;
    ONG() = default;
    void add(unique_ptr<Participant> part) {

        part->tipareste();
 
        ls.emplace_back(std::move(part));

        for (const auto& i : ls) {
            i->tipareste(); 
        }
        
    }
};

这是完整的代码:

#include <iostream>
#include <assert.h>
#include <vector>
#include <memory>
#include <variant>
#define _CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>

using namespace std;

struct AtExit
{
    ~AtExit() { _CrtDumpMemoryLeaks(); }
} doAtExit;


class Participant {

public:
    virtual void tipareste() = 0;
    bool eVoluntar = true;
    virtual ~Participant() = default;
};
class Personal : public Participant {
private:
    string nume;
public:
    Personal() = default;
    Personal(string n) :nume{ n } {}
    void tipareste() override {
        cout << nume;
    }
};

class Administrator : public Personal {
public:
    std::unique_ptr<Personal> pers;
    Administrator() = default;
    Administrator(Personal* p) : pers{ p } {}
    void tipareste() override {
        cout << "administrator ";
        pers->tipareste();
    }
};

class Director : public Personal {
public:
    std::unique_ptr<Personal> pers;
    Director() = default;
    Director(Personal* p) : pers{ p } {}
    void tipareste() override {
        cout << "director ";
        pers->tipareste();
    }
};

class Angajat :public Participant {
public:
    std::unique_ptr<Participant> participant;
    Angajat() = default;
    Angajat(Participant* p) : participant{ p } { this->eVoluntar = false; /*p->eVoluntar = false;*/ }
    void tipareste() override {
        cout << "anjajat ";
        participant->tipareste();
    }
};

class ONG : public Participant {
private:
    
public:

    ONG() = default;
    std::vector<unique_ptr<Participant>> ls;    
   void add(unique_ptr<Participant> part) {
 
        ls.emplace_back(std::move(part));
        
    }
};


int main() {


    ONG* ong{};

    std::unique_ptr<Participant> aba = std::unique_ptr<Personal>(new Personal("Will"));
    ong->add(std::move(aba));
    
   
}

【问题讨论】:

  • 请比“它不起作用”和“它给我一个错误”更具体。
  • 你得到的错误是什么?
  • 一个问题是main 中的ong 没有指向ONG 实例。为什么ong 是指针?
  • 另外一个问题是ONG继承自Participant,但没有实现virtual void tipareste() = 0;

标签: c++ vector polymorphism smart-pointers


【解决方案1】:

问题在于 ONG 类是一个抽象类,因为它继承自具有纯虚函数的 Participant。

如果你定义

    void tipareste() {
 //Do stuff
    }

ONG 内(或在ONG 继承的类内) 然后将ONG 对象分配给ONG 指针

int main() {

    std::shared_ptr<ONG> ong = std::make_shared<ONG> ();

    std::unique_ptr<Participant> aba = std::unique_ptr<Personal>(new Personal("Will"));
    ong->add(std::move(aba));

}

可以跑

【讨论】:

  • 请不要使用new。明确地将其替换为make_unique/make_shared
  • @GeorgianGherasim 是的,您可以通过这种方式分配给 ong 指针。还是需要定义纯虚函数void tipareste()
猜你喜欢
  • 2017-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-02
  • 2019-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多