对静态重载 (class) function/method 的调用不明确
Call to static overloaded (class) function/method is ambiguous
在我的 C++ class 中,我有两个静态方法,称为 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) 版本中的一个错误,所以没有真正的解决办法,只能等到我可以使用(更)更新的编译器版本。
感谢所有提供帮助和建议的人。
在我的 C++ class 中,我有两个静态方法,称为 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) 版本中的一个错误,所以没有真正的解决办法,只能等到我可以使用(更)更新的编译器版本。
感谢所有提供帮助和建议的人。