首页 > 解决方案 > 使用现有的类成员指针从类外部调用函数

问题描述

我遇到了一个与智能指针相关的问题。我有一个继承自类Cell的类Property ,我想将当前类成员指针传递给Player类成员函数。

class Player {
private:
    std::vector<std::shared_ptr<Cell>> cells;
    int money;
public:
    
    bool buysProperty(std::shared_ptr<Cell> property)
    {
         //some code in which i would like to call both Cell and Property functions
    }    
};

这就是我从 Property 类内部调用此函数的方式:

class Property : public Cell {
private:

public:
    void action(Player* currentPlayer) {
        currentPlayer->buysProperty(std::make_shared<Property>(this)) {
        }
    }
};

编译后我得到这个错误: 'Property::Property(const Property &)': cannot convert argument 1 from '_Ty' to 'const Property &'

我不知道出了什么问题,是我调用函数错误,还是函数体有问题?也许是“this”关键字问题?谢谢你所有的时间和答案:)

标签: c++ooppointersinheritancesmart-pointers

解决方案


推荐阅读