开发者

Constructor for class 'QSharedPointer<T>' is declared 'explicit' - How to resolve this error

开发者 https://www.devze.com 2023-02-18 06:37 出处:网络
I get the following error when trying to use: typedef QSharedPointer<Test> CTest CTest* Module::function(params)

I get the following error when trying to use:

typedef QSharedPointer<Test> CTest


CTest* Module::function(params)
{
    CTestNew* ptr = new CTestNew(params);

    dosomething();

    return ptr;
}

Then replace Test* with CTest in the code. What am I missing?

error C2664: 'QSharedPointer<T>::QSharedPointer(const QSharedPointer<T> &)' : cannot convert parameter 1 from 'CTestNew*' to 'const QSharedPointer<T> &'
            with
            [
               T=Test
            ]
            Reason: cannot convert from 'CTestNew *' to 'const QSharedPointer<T>'
            with
            [
                T=Test
   开发者_开发知识库        ]
            Constructor for class 'QSharedPointer<T>' is declared 'explicit'
            with
            [
                T=Test
            ]


Compiler error's saying that CTestNew isn't the same as Test

EDIT: In response to comments saying CTestNew is a subclass of the abstract Test

CTest* Module::function(params)
{
    CTestNew* ptr = new CTestNew(params);
    dosomething();
    return ptr;
}

should be:

CTest Module::function(params) // Don't return a pointer to a shared pointer
{
    Test * ptr = new Test(params); // You're using Test not CTestNew in the CTest typedef
    dosomething();
    return CTest(static_cast<Test *>(ptr));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消