0

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
            ]
demonplus
  • 5,613
  • 12
  • 49
  • 68
Vij
  • 27
  • 1
  • 7

1 Answers1

0

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));
}
Erik
  • 88,732
  • 13
  • 198
  • 189
  • Don't use `CTestNew` where you need to use `Test`. – Erik Mar 22 '11 at 19:29
  • @Ronny: He's doing `CTest foo(new CTestNew)` or something similar, where he should do `CTest foo(new Test)` - At least that's what the compiler error claims. – Erik Mar 22 '11 at 21:00
  • I see. In this case you'd be right. He needs to clarify this. – Ronny Brendel Mar 22 '11 at 21:08
  • @Rony/Erik: Thanks for your comments. There is a function Test* function(parameters) in which a pointer to CTestNew is created. CTestNew * ptr= dosomething() and this function returns CTestNew. It worked prior to me adding the typedef because CTestNew is inherited from Test. Should I do a typecast in this case? – Vij Mar 22 '11 at 22:06
  • CTest* Module::function(params) { CTestNew* ptr = new CTestNew(params); dosomething(); return ptr; } – Vij Mar 22 '11 at 22:17
  • @VIJ: use `CTest Module::function(params) { Test * ptr = new Test; ... return ptr; }` – Erik Mar 22 '11 at 22:20
  • Thanks. I cannot instantiate Test because it is an abstract class. I use the return type CTest . Also, CTestNew inherits from Test. CTestNew:public Test .Should I use casting? – Vij Mar 22 '11 at 22:30
  • In that case you need to `return CTest(static_cast(ptr));` – Erik Mar 22 '11 at 22:31
  • When I use a 'go to definition' or F12 in my CTestNew which is the newly introduced typedef, I get an error 'symbol not defined'.However, it is not the case with other parts of the code.I have done a clean rebuild.What could be the reason? – Vij Mar 22 '11 at 22:49
  • @VIJ: No idea - that's a completely different question, you should post a new one. – Erik Mar 22 '11 at 22:50