Vij
Vij

Reputation: 27

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

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
            ]

Upvotes: 0

Views: 1073

Answers (1)

Erik
Erik

Reputation: 91270

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));
}

Upvotes: 0

Related Questions