0

尝试使用时出现以下错误:

typedef QSharedPointer<Test> CTest


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

    dosomething();

    return ptr;
}

然后在代码中用 CTest 替换 Test*。我错过了什么?

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
            ]
4

1 回答 1

0

编译器错误的说法CTestNew不一样Test

编辑:回应评论说CTestNew是摘要的子类Test

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

应该:

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));
}
于 2011-03-22T19:20:22.853 回答