错误C2679:二进制'=':找不到运算符,该运算符采用类型为'kingMobile::KingChatFilter *'的右侧操作数(或者没有可接受的转换)

这是playGame.cpp

filter = new KingChatFilter;  // here is the error line

并在其.h中
spKingChatFilter filter;

和KingChatFilter.h
class KingChatFilter : public boost::enable_shared_from_this<KingChatFilter> {
        public:

            KingChatFilter();
            string filter(string msg);

        private:

    };

    typedef boost::shared_ptr<KingChatFilter> spKingChatFilter;

再次,我从c++开始,我试图理解为什么它不起作用...感谢您的耐心等待...

最佳答案

指向托管类型的指针的 shared_ptr constructorexplicit

template<class Y> explicit shared_ptr(Y * p);

这可以防止在以下分配中从KingChatFilter *返回的new隐式转换为shared_ptr<KingChatFilter>:
filter = new KingChatFilter;

首选的解决方案是将任务替换为
filter = boost::make_shared<KingChatFilter>();

另一个可行的解决方案是
filter = spKingChatFilter(new KingChatFilter);

10-06 13:15