[英]Call to static overloaded (class) function/method is ambiguous
在我的C ++类中,我有两个静态方法,称为getInstance。
方法声明如下:
public: // +++ STATIC +++
static CanCommunicator* getInstance(shared_ptr<ThreadState> state);
static CanCommunicator* getInstance();
在全局函数中(由于遗留代码需要),我从对象调用getter:
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;
}
感谢Radoslaw的建议,我只是将违规函数重命名为getInstanceByState()。
这似乎是我正在使用的GCC版本(4.4.5)中的一个错误,所以没有真正的修复,但要等到我可以使用(更多)更新的编译器版本。
感谢所有帮助过的人并给出了建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.