question

flaviu avatar image
0 Votes"
flaviu asked flaviu commented

Circular include problem

I have entered into a circular include problem. I have a class:


 #include "TestConnection.h"
 #include "oci.h"
 #include "SU_ORADB_imp.h"
    
 class DBTestConnection final : public Connection
 {
 private:
     SU_ORADB_imp* imp_{ nullptr };
     bool m_connected = false;
     // methods
     DBTestConnection();
     // friend class
     friend ConnectionPoolFactory<DBTestConnection>;
    
 public:
     void disconnect() override;
     bool connect() override;
     void do_something();
     ~DBTestConnection() override;
     OCISvcCtx* GetServerContext() { return imp_->_svchp; }
 };

and another class:

 #pragma once
    
 #include "oci.h"
 #include "ConnectionFactory.h"
    
 class SU_ORADB_imp
 {
 private:
     OCISvcCtx* _svchp;    // just for testing purpose
     // methods
     void Init();
    
 protected:
     PoolProxy GetConnection();
     static std::unique_ptr<ConnectionPool> pool_;
    
 public:
     void Connect();
     void Disconnect();
     SU_ORADB_imp();
     ~SU_ORADB_imp();
     void DoSqlSelect();
    
     friend class DBTestConnection;
 };

but I got these errors:

 dbtestconnection.h(10): error C2143: syntax error: missing ';' before '*'
 dbtestconnection.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
 dbtestconnection.h(10): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
 dbtestconnection.h(24): error C2065: 'imp_': undeclared identifier

and

 connectionfactory.h(11): error C2065: 'DBTestConnection': undeclared identifier
 connectionfactory.h(12): error C2923: 'ConnectionPoolFactory': 'DBTestConnection' is not a valid template type argument for parameter 'T'
 connectionfactory.h(27): error C2913: explicit specialization; 'ConnectionPoolFactory' is not a specialization of a class template

How can I get rid of this issue ?

Here I attached a sample project: https://file.io/YGelycLwYWNE










c++
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The error messages relate to two of your header files. You have not provided the code for either. The two headers are also not present in the link you provided.

You need to show us the code in the headers or at least a significant portion of the code near the line numbers specified in the error messages.

0 Votes 0 ·
flaviu avatar image
0 Votes"
flaviu answered

I prepared a simple test project: https://1drv.ms/u/s!AtSPCxnvttzehGyK1Kj0Vv9eS4ab?e=wTF8mR

The error remained is:

 connectionfactory.h(8): error C2065: 'DBTestConnection': undeclared identifier
 connectionfactory.h(9): error C2923: 'ConnectionPoolFactory': 'DBTestConnection' is not a valid template type argument for parameter 'T'
 connectionfactory.h(23): error C2913: explicit specialization; 'ConnectionPoolFactory' is not a specialization of a class template

from this header file:

 #pragma once
    
 #include "connection.h"
 #include "pool.h"
 #include "DBTestConnection.h"
    
 template <>
 class ConnectionPoolFactory<DBTestConnection>  // <--- error
 {
 public:
     static std::unique_ptr<ConnectionPool> create(const std::uint16_t& num_connections)
     {
         std::vector<std::unique_ptr<Connection>> connections;
         for (std::uint16_t k = 0; k < num_connections; ++k)
         {
             connections.emplace_back(std::unique_ptr<DBTestConnection>(new DBTestConnection{}));
         }
         return std::unique_ptr<ConnectionPool>(new ConnectionPool{ std::move(connections) });
     }
    
 private:
     ConnectionPoolFactory() = default;
 };

How can I solve it ?


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

flaviu avatar image
0 Votes"
flaviu answered

I have solved by moving DBTestConnection definition into ConnectionPoolFactory header.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.