我有一個鏈接錯誤,其中連接器抱怨我的具體類的析構函數正在調用其抽象超類析構函數,其代碼丟失。鏈接器錯誤:想C++虛擬基類析構函數
這是在XCode的Mac OS X上使用GCC 4.2。
我看到了g++ undefined reference to typeinfo但它不是完全相同的東西。
這裏是接頭錯誤消息:
Undefined symbols:
"ConnectionPool::~ConnectionPool()", referenced from:
AlwaysConnectedConnectionZPool::~AlwaysConnectedConnectionZPool()in RKConnector.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
這裏是抽象基類聲明:
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;
};
這裏是混凝土類聲明:
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);
};
不用說,所有這些成員都得到執行。這裏是析構函數:
AlwaysConnectedConnectionZPool::~AlwaysConnectedConnectionZPool()
{
printf("AlwaysConnectedConnectionZPool destructor call"); // nothing to destruct in fact
}
,也或許工廠例行:
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);
}
我可以人爲地讓我的抽象基類,具體解決此問題。但我寧願做更好的事情。任何想法?
感謝
您的回答暗示,但值得強調的是,在C++中,抽象方法*可以具有實現。當我發現時,我真的很驚訝,在此之前,我一直認爲方法不是抽象的就是有實現,而不是兩者。 – sbk 2010-03-31 17:32:30
@sbk這是不正確的。在C++中抽象意味着_pure_虛擬。一個_non-pure_虛函數不是抽象的,也不是抽象的。 – Zimano 2016-01-07 11:40:16
@Zimano:我評論的哪部分內容不正確?我同意「純虛擬」和「抽象」是一回事。我在說,在C++中,抽象/純虛擬方法*可以具有body/implementation和destructor,即使是抽象的,也必須具有一個。即片段'''class A {virtual〜A()= 0 {}}'''是有效的 – sbk 2016-01-13 12:44:32