在我的C ++类中,我有两个静态方法,称为getInstance。
方法声明如下:
public: // +++ STATIC +++
static CanCommunicator* getInstance(shared_ptr<ThreadState> state);
static CanCommunicator* getInstance();
在全局函数中(由于遗留代码而需要),我正在从对象中调用吸气剂:
auto tState = CanCommunicator::getInstance()->getThreadState();
编译器(GCC 4.4.5)给我以下错误:
CanCommunicator::getInstance is ambiguous
Candidates are:
CanCommunicator * getInstance()
CanCommunicator * getInstance(std::shared_ptr<IsoTpThreadState>)
是什么原因导致此错误,我该如何解决? 实例创建需要重载的方法,无参数的方法用于纯实例检索。
编辑:根据要求提供更多示例代码。
#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;
}
答案 0 :(得分:0)
由于Radoslaw的建议,我只是将有问题的函数重命名为getInstanceByState()。
这似乎是我使用的GCC(4.4.5)版本中的错误,因此没有真正的解决方法,只能等到我可以使用(很多)较新的编译器版本。
感谢所有提供帮助和建议的人。