从[母班]到[子班]的参数1没有已知的转换

时间:2014-11-20 13:25:08

标签: c++ class type-conversion

这是我的问题,我尝试将[Mother class]的内容复制到[Child class]中,如下所示:


    class Trick
    {
    public:
        Trick();

    protected:
        std::string _name;
        int _point;
    };

    class TrickGraph : public Trick
    {
    public:
        TrickGraph();

    protected:
        sf::Vector2i _pos;
        sf::Vector2u _size;
    };

    class Sheet
    {
    public:
        Sheet()
        {
            _trick = NULL;
        }

        void set_trick(const Trick *trick)
        {
             if (_trick)
                 delete _trick;
            _trick = new TrickGraph();
            *(_trick) = *trick;
        }

    protected:
        TrickGraph *_trick;
    };

之后,g ++在*(_trick) = *trick;行显示此错误:


`In member function ‘void Sheet::set_trick(const Trick*)’:`
`error: no match for ‘operator=’ in ‘*((Sheet*)this)->Sheet::_trick = * trick’`
`note: candidate is:`
`note: TrickGraph& TrickGraph::operator=(const TrickGraph&)`
`note:   no known conversion for argument 1 from ‘const Trick’ to ‘const TrickGraph&’`
`make: *** [...] Error 1`

我尝试了很多东西来解决它,但正如你所看到的那样,我发布了堆栈溢出... 所以,如果你能帮助我,那就太棒了:D

1 个答案:

答案 0 :(得分:-1)

最后,我做了另一种有效的方式。我在TrickGraph中设置了Trick * _trick属性,顺便说一句,它不是Trick的孩子。我不得不改变一些东西以使其有效但是......它有效!

感谢n.m。