简体   繁体   English

智能指针构造函数错误

[英]Smart pointer constructor error

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 在下面的例子中,我打算将B类作为指向A类的智能指针。我得到以下链接器错误

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. 我不清楚应该在构造函数中从类A传递什么。 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();

    }

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? 您是否为B::B(A*)构造函数提供了实现? 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. 如果是,那么可以解释这两个问题的答案是“不”,那么你应该能够通过在实现中提供和链接来轻松解决这个问题。

If you are just trying to use a smart pointer then should not be trying to inherit from QSharedPointer you want something like 如果您只是尝试使用智能指针,那么不应该尝试从QSharedPointer继承您需要的东西

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. 您可以查看scoped_pointer的boost实现,以获得基本智能指针的相当容易理解的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM