0

I've ask this question, and I still can't solve the problem on my project.

My project uses third-party library, Hazelcast C++ Client.

The use of Hazelcast Client is like:

#include "hazelcast/client/HazelcastAll.h"

using namespace hazelcast::client;

int main(){
    ClientConfig clientConfig;
    Address address("192.168.85.34", 5701);
    clientConfig.getNetworkConfig().addAddress(address);
    HazelcastClient hz(clientConfig);

    // do something...

    hz.shutdown();
    return 0;
}

This code is no problem. More examples can be gotten in its website. However, if I want to make HazelcastClient hz as a class private member, I have no idea to instantiate my class !!

I've checked out the HazelcastClient constructor in HazelcastClient.h header, the constructor use ClientConfig &config as parameter, which let the following code cause error like:

HzService.cpp: error: cannot bind non-const lvalue reference of type ‘hazelcast::client::ClientConfig&’ to an rvalue of type ‘hazelcast::client::ClientConfig’

It's because the compilation message has 14282 lines, which is too many to paste on here, so I just paste an important error above! The full compilation message can be download here.

#include "hazelcast/client/HazelcastAll.h"
using namespace hazelcast::client;

class HzService {
public:
    HzService();
    virtual ~HzService();

private:
    // Variables:
    HazelcastClient hz_client;

    // Functions:
    ClientConfig createLocalConfig();
};

HzService::HzService()
    :hz_client(createLocalConfig()){
}

HzService::~HzService() {
    hz_client.shutdown();
}

ClientConfig HzService::createLocalConfig() {
    ClientConfig clientConfig;
    Address address("192.168.85.34", 5701);
    clientConfig.getNetworkConfig().addAddress(address);
    return clientConfig;
}

int main () { 
    HzService hs;
    return 0;
}

Maybe that's not the Hazelcast problem, but the question if I use third-party library that its class constructor use non-const reference as parameters, how can I make it into my class private member?

If the issue is specifically related to Hazelcast, I'll change the title.



EDIT:

If someone need to test the code, please download the Hazelcast C++ Client first, and use following command to compile (The content of test.cpp is the code above) :

g++ -std=c++11 \
    -I/path/to/hazelcast-cpp-client/hazelcast/include \
    -I/path/to/hazelcast-cpp-client/external/include/ \
    -I/path/to/hazelcast-cpp-client/hazelcast/generated-sources/include \
    test.cpp \
    /path/to/hazelcast-cpp-client/release/libHazelcastClient3.11_64.a \
    -lpthread
Community
  • 1
  • 1
hsuchengmao
  • 524
  • 1
  • 6
  • 14
  • 2
    One way would be to make the `ClientConfig` a member variable. Initialize it first then pass it to the `HazelcastClient` constructor. You could also use a smart pointer to hold the `HazelcastClient` instance which gives you more flexibility on when you need to have the `ClientConfig` available. You just need to make sure it's not a temporary you pass in, but I'd also be sure that the lifetime of the config doesn't need to exceed the lifetime of the client. I've seen such terrible API designs before. – Retired Ninja Jan 04 '19 at 05:55
  • First of all, please edit your question to include a copy-paste (as text) of the *full* and *complete* error output. There's usually more output or informational notes that can help in figuring things out. Secondly, which line is line `46` in the file `HzService.cpp`? Lastly, please try to create a [mcve] to show us, something we can copy-paste and try for ourselves. – Some programmer dude Jan 04 '19 at 06:00

1 Answers1

1

Declare hz_client as a pointer instead.
Declare ClientConfig as a pointer as well

In HzService() first create the clientconfig instance, then create hz_client passing the instance of clientconfig into it.

E.g.

class HzService
{
...
HazelcastClient* hz = nullptr;  // but better with smart pointer and custom deleter.
std::unique_ptr<ClientConfig> cc;
...

HzService()
{
  cc = std::make_unique<ClientConfig>();  
  Address address("192.168.85.34", 5701);
  cc->getNetworkConfig().addAddress(address);
  hz_client = new HazelcastClient(*cc.get());
}

then add some code in the destructor to do shutdown and delete hz_client

AndersK
  • 35,813
  • 6
  • 60
  • 86