简体   繁体   English

链接器错误:想要C ++虚拟基类析构函数

[英]Linker error: wants C++ virtual base class destructor

I have a link error where the linker complains that my concrete class's destructor is calling its abstract superclass destructor, the code of which is missing. 我有一个链接错误,其中链接器抱怨我的具体类的析构函数正在调用其抽象的超类析构函数,其代码缺失。

This is using GCC 4.2 on Mac OS X from XCode. 这是在XCode的Mac OS X上使用GCC 4.2。

I saw g++ undefined reference to typeinfo but it's not quite the same thing. 我看到了g ++未定义的对typeinfo的引用,但它并不完全相同。

Here is the linker error message: 这是链接器错误消息:

Undefined symbols:
  "ConnectionPool::~ConnectionPool()", referenced from:
      AlwaysConnectedConnectionZPool::~AlwaysConnectedConnectionZPool()in RKConnector.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

Here is the abstract base class declaration: 这是抽象基类声明:

class ConnectionPool {
public:
    static ConnectionPool* newPool(std::string h, short p, std::string u, std::string pw, std::string b);   
    virtual ~ConnectionPool() =0;
    virtual int keepAlive() =0;
    virtual int disconnect() =0;
    virtual sql::Connection * getConnection(char *compression_scheme = NULL) =0;
    virtual void releaseConnection(sql::Connection * theConnection) =0;
};

Here is the concrete class declaration: 这是具体的类声明:

class AlwaysConnectedConnectionZPool: public ConnectionPool {
protected:
    <snip data members>
public:
    AlwaysConnectedConnectionZPool(std::string h, short p, std::string u, std::string pw, std::string b);   
    virtual ~AlwaysConnectedConnectionZPool();
    virtual int keepAlive();    // will make sure the connection doesn't time out. Call regularly
    virtual int disconnect();   // disconnects/destroys all connections.
    virtual sql::Connection * getConnection(char *compression_scheme = NULL);
    virtual void releaseConnection(sql::Connection * theConnection);
};

Needless to say, all those members are implemented. 毋庸置疑,所有这些成员都得到了实施。 Here is the destructor: 这是析构函数:

AlwaysConnectedConnectionZPool::~AlwaysConnectedConnectionZPool()
{
    printf("AlwaysConnectedConnectionZPool destructor call");  // nothing to destruct in fact
}

and also maybe the factory routine: 也许是工厂例行程序:

ConnectionPool* ConnectionPool::newPool(std::string h, short p, std::string u, std::string pw, std::string b)
{
    return new AlwaysConnectedConnectionZPool(h, p, u, pw, b);
}

I can fix this by artificially making my abstract base class concrete. 我可以通过人为地使我的抽象基类具体化来解决这个问题。 But I'd rather do something better. 但我宁愿做更好的事情。 Any idea? 任何的想法?

Thanks 谢谢

Even if you declare a destructor as a pure virtual function, you must provide an implementation for it. 即使将析构函数声明为纯虚函数,也必须为其提供实现。 Although you cannot instantiate an abstract class directly, it is always instantiated when you instantiate one of its derived (concrete) classes. 虽然您无法直接实例化抽象类,但在实例化其派生(具体)类之一时,它始终会被实例化。 And so at some point such instances will be destroyed, thus requiring a destructor. 因此在某些时候这样的实例将被破坏,因此需要一个析构函数。 The implementation of the pure virtual destructor can be (and normally is) an empty function: 纯虚析构函数的实现可以(通常是)一个空函数:

ConnectionPool::~ConnectionPool() {
}

Even in an abstract class, you don't want your destructor to be pure virtual. 即使在抽象类中,您也不希望析构函数是纯虚拟的。 That's because it is going to get called when a concrete subclass' destructor is called. 那是因为当调用一个具体的子类'析构函数时它会被调用。

We use the following pattern. 我们使用以下模式。

foo.h foo.h中

class AbstractBaseClass {
public:
    virtual ~AbstractBaseClass();
    virtual void method1() = 0;
    virtual void method2() = 0;
protected:
    AbstractBaseClass() {
    }
};

foo.cpp Foo.cpp中

AbstractBaseClass::~AbstractBaseClass() {
}

See this FAQ for details . 有关详情,请参阅此FAQ

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

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