0

I am trying to understand the usage of smart pointers. In the below example, I intend Class B to be the smart pointer to class A. I get the following linker error

error LNK2019: unresolved external symbol "public: __thiscall ClassB::ClassB(classA *)"

I seem to be missing something with the constructor. I am not clear as to what should be passed from class A in the constructor. I would appreciate if somebody could explain.

 class A 
    {

     friend  class B;
    virtual methods ();

    protected:
    virtual ~A();

    }

    class B:public QSharedPointer<A>
    {
       B();
       B(A * pData);
       B(const  B &data);
      virtual ~  B();

    }
2
  • Is that the actual code and linker error, verbatim? Your class is called B but the linker error references ClassB, which seems very strange. Commented Mar 23, 2011 at 0:05
  • syntax errors, care to modify? miss ';', miss definition of most mem functions. Commented Mar 23, 2011 at 2:45

2 Answers 2

2

The error you're getting is a linker error, not a compiler error, which happens (among other cases) when you prototype a function but don't implement it. Did you provide an implementation for your B::B(A*) constructor? If so, did you compile and link it in to the resulting executable? If thenot, that would explain the answer to either question is "no," then you should easily be able to fix this by providing and linking in an implementation.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are just trying to use a smart pointer then should not be trying to inherit from QSharedPointer you want something like

QSharedPointer<A> ptr(new A());
ptr->do_something();

If you are trying to implement your own smart pointer then you still most likely to not want to inherit from some other smart pointer class. You can take a look at the boost implementation of scoped_pointer for a fairly easy to understand implementation of a basic smart pointer.

10 Comments

The class A is an abstract class and many clients call a function returning A* currently. I would like to use a smart pointer , so that the clients dont have to the delete operation. Is it not a correct approach to inherit from QSharedpointer for this purpose ?
@VIJ: Why do you want to inherit it, rather than, say, typedef QSharedPointer<A> B;?
I would say that it is defintely not the way to go. As aschepler points out the typedef does what you want. Generally you would want to inherit is if you want to change the behavior of the base class in some way which I do not believe you want in this instance.
BTW, not having to call delete is really a very minor benefit of using smart pointer. First and foremost smart pointers are a generalized way to place the delete call in a destructor. This is the only way to be sure that you program does not leak resources when an exception is thrown. As a rule of thumb, anytime that you see a call to delete and it is not in a destructor there is a memory leak there, because almost always there will be something between the new and the delete that can throw
@ltc Thankyou for the comments. As I understand, QSharedpointer would already provide the functionality to delete . So typedef QSharedPointer<A> B should help avoid memory leaks. You mention about placing the delete call in the destructor. This is not done explicitly when using QSharedPointer from user code. How does the QSharedpointer achieve this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.