In my C++ class, I have two static methods, called getInstance.
The method declarations are as follows:
public: // +++ STATIC +++
static CanCommunicator* getInstance(shared_ptr<ThreadState> state);
static CanCommunicator* getInstance();
In a global function (required, because of legacy code), I'm calling a getter from the object:
auto tState = CanCommunicator::getInstance()->getThreadState();
The compiler (GCC 4.4.5) is giving me the following error:
CanCommunicator::getInstance is ambiguous
Candidates are:
CanCommunicator * getInstance()
CanCommunicator * getInstance(std::shared_ptr<IsoTpThreadState>)
What is causing this error and how can I fix it? The overloaded method is required for instance creation, the method without parameters is used for pure instance retrieval.
EDIT: As per request more code for the example.
#include <memory>
#include <stdint.h>
#include <linux/can.h>
#include <linux/can/raw.h>
using std::shared_ptr;
//================
// Class in question
//================
struct ThreadState {
int32_t socketFd;
uint32_t sendId;
};
class CanCommunicator {
#define null NULL
public: // +++ STATIC +++
static CanCommunicator* getInstance(shared_ptr<ThreadState> state);
static CanCommunicator* getInstance();
public:
shared_ptr<ThreadState> getThreadState() { return this->threadState; }
protected:
CanCommunicator(shared_ptr<ThreadState> state);
private: // +++ STATIC +++
static CanCommunicator* instance;
private:
shared_ptr<ThreadState> threadState;
};
/*
+++++++++++++++++++++++++++++++++++++
+++ STATIC VARIABLES +++
+++++++++++++++++++++++++++++++++++++
*/
CanCommunicator* CanCommunicator::instance = null;
/*
+++++++++++++++++++++++++++++++++++++
+++ STATIC METHODS +++
+++++++++++++++++++++++++++++++++++++
*/
CanCommunicator* CanCommunicator::getInstance(shared_ptr<ThreadState> state) {
if (state == null && instance == null)
throw "Cannot instantiate from nullptr!";
return instance == null ? (instance = new CanCommunicator(state)) : instance;
}
CanCommunicator* CanCommunicator::getInstance() { return instance == null ? throw "No instance found!" : instance; }
CanCommunicator::CanCommunicator(shared_ptr<ThreadState> state) {
this->threadState = state;
}
//================
// Calling code
//================
int canSendData(int32_t skt, uint32_t sendCanId, const uint8_t* data, uint8_t dataLength, uint8_t extended) {
struct can_frame myCanFrame;
return (int)write(skt, &myCanFrame, sizeof(myCanFrame));
}
int isotp_user_send_can(const uint32_t arbitrationId, const uint8_t* data, const uint8_t size) {
auto tState = CanCommunicator::getInstance()->getThreadState(); //<-------- Error originates here
return canSendData(tState->socketFd, (int)tState->sendId, data, size, false) > 0 ? 0 : 1;
}
Thanks to Radoslaw's suggestion, I simply renamed the offending function to getInstanceByState().
This seems to be a bug in the version of GCC (4.4.5) I'm using, so there is no true fix but to wait until I can use a (much) newer compiler version.
Thank you to all who helped and gave suggestions.