I have successfully packaged the library this creates under namespace `cpool`, via conan.. now I also have written my customized Connection Pool .. but I am unclear how to instantiate it properly in my project code
Main.cpp - this is where I attempt to instantiate the Connection Pool via the factory.. but it doesn't like it.. any idea why? or tips is greatly appreciated
1 2 3 4 5 6 7 8 9 10 11 12
#include <connection-pool/pool.h>
#include "TMAConnection.h"
class ServerHandler : public myserverIf {
public:
ServerHandler(int num_of_connections) {
auto tma_conn_pool = cpool::ConnectionPoolFactory<TMAConnection>::create(4);
}
...
}
the error I am getting when attempting to compile is this
undefined reference to `cpool::ConnectionPoolFactory<TMAConnection>::create(unsigned short)
Just like templates, template specializations also need to be in the 'header' (that is, full definition before being called) so that the full definition is known when the compiler generates code from a template.
Your template specialization for cpool::ConnectionPoolFactory<TMAConnection> is in a .cpp file so your Main.cpp isn't going to see it.
________________________________________
It's similar to the same reason why the following won't compile:
Thank you Ganado for responding . I realized that cpool has no such class ConnectionPoolFactory.. and have since backed off that namespace .. and based on your feedback.. I backed off trying to get this into a .cpp and .h file and just use the classes for now within my Main.cpp ... now getting a different error
-- Conan: Compiler GCC>=5, checking major version 9
-- Conan: Checking correct version: 9
-- Configuring done
-- Generating done
-- Build files have been written to: //build_cmake
Scanning dependencies of target server_server
[ 16%] Building CXX object CMakeFiles/server_server.dir/src/server_server.cpp.o
//src/server_server.cpp:41:12: error: ‘ConnectionPoolFactory’ does not name a type
41 | friend ConnectionPoolFactory<TMAConnection>;
| ^~~~~~~~~~~~~~~~~~~~~
//src/server_server.cpp:47:7: error: ‘ConnectionPoolFactory’ is not a class template
47 | class ConnectionPoolFactory<TMAConnection> {
| ^~~~~~~~~~~~~~~~~~~~~
//src/server_server.cpp: In instantiation of ‘static std::unique_ptr<cpool::ConnectionPool> ConnectionPoolFactory<TMAConnection>::create(uint16_t) [with TMAConnection = TMAConnection; uint16_t = short unsigned int]’:
//src/server_server.cpp:65:64: required from here
//src/server_server.cpp:53:71: error: ‘constexpr TMAConnection::TMAConnection()’ is private within this context
53 | connections.emplace_back( std::unique_ptr<TMAConnection>( new TMAConnection{} ) );
| ^~~~~~~~~~~~~~~~~~~
//src/server_server.cpp:40:5: note: declared private here
40 | TMAConnection() = default;
| ^~~~~~~~~~~~~
//src/server_server.cpp:55:56: error: ‘cpool::ConnectionPool::ConnectionPool(std::vector<std::unique_ptr<cpool::Connection> >&&)’ is protected within this context
55 | return std::unique_ptr<cpool::ConnectionPool>( new cpool::ConnectionPool{std::move( connections )} );
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from //src/server_server.cpp:9:
/home/ubuntu/.conan/data/connpool/1.0.0/bonk/prod/package/abd3ca9581f5ec3d6672fa2ee8818b1f09dbb082/include/connection-pool/pool.h:57:14: note: declared protected here
57 | explicit ConnectionPool( std::vector< std::unique_ptr< Connection > >&& connections );
| ^~~~~~~~~~~~~~
make[2]: *** [CMakeFiles/server_server.dir/build.make:63: CMakeFiles/server_server.dir/src/server_server.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/server_server.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
chmod: cannot access 'server_server': No such file or directory
I wish I knew how to get these classes into their own .cpp and .h
You can put the template/template specializations into their own header, but you can't separate out template code into a .cpp file. You can separate out your TMAConnection class since it's not a class template. But you can't separate out your ConnectionPoolFactory.
Remember: #including a header is just a crude copy-paste mechanism that the pre-processor does. Nothing more.