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

StackOverflow https://stackoverflow.com/questions/5396554

  •  28-10-2019
  •  | 
  •  

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));
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top