Forward declaration only tells the compiler "such a class exists". In your
AutoConn getConn()
since AutoConn
is a value type, the whole structure of AutoConn
must be known, so forward declaration of the class won't work. So you must put the actual declaration of AutoConn
before ConnectionPool
.
In your AutoConn
, the type ConnectionPool
is only referred by pointers. In this case the whole structure of ConnectionPool
is not required, so forward declaration of ConnectionPool
is enough.
Therefore you need to rearrangement the classes into this:
class Connection;
class ConnectionPool;
class AutoConn { ... };
class ConnectionPool { ... };
But notice that
AutoConn(ConnectionPool* pool, Connection *c) : m_pool(pool), m_connection(c) {}
~AutoConn()
{
m_pool->releaseConnection(m_connection);
}
these methods require the compiler to know the members of ConnectionPool
, so a complete structure is needed. To solve this problem the definition must be placed after ConnectionPool
. Thus only the constructors and destructors should remain.
class AutoConn {
...
AutoConn(ConnectionPool* pool, Connection *c);
~AutoConn();
}
class ConnectionPool { ... };
AutoConn::AutoConn(ConnectionPool* pool, Connection *c) : ... { ... }
AutoConn::~AutoConn() { ... }