Circular include problem

Flaviu_ 1,031 Reputation points
2021-05-11T17:06:07.73+00:00

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

Developer technologies C++
{count} votes

2 answers

Sort by: Most helpful
  1. Flaviu_ 1,031 Reputation points
    2021-05-12T07:41:16.727+00:00

    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 ?

    0 comments No comments

  2. Flaviu_ 1,031 Reputation points
    2021-05-12T11:03:45.923+00:00

    I have solved by moving DBTestConnection definition into ConnectionPoolFactory header.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.