背景:我正在使用委託技術來抽象訪問任意對象方法,但我在鏈接器方面存在一些問題。考慮以下課程,ContextNode
。無法解析模板委託中的重載類方法
template <class ObjectType, class GetType, class SetType>
class ContextNode: public ContextNodeBase {
public:
ContextNode(ObjectType* target,
GetType (ObjectType::*getter)(void),
void (ObjectType::*setter)(const SetType& ref)
): _target(target), _getter(getter), _setter(setter) { }
virtual ~ContextNode(void) { }
virtual void r(Datum& value) {
value = (_target->*_getter)();
return;
}
virtual void w(const Datum& value) {
(_target->*_setter)(value);
return;
}
private:
ObjectType* _target;
GetType (ObjectType::*_getter)(void);
void (ObjectType::*_setter)(const SetType& ref);
};
Datum
的執行是無關緊要的。也考慮微不足道的類Thing
。
class Thing {
public:
Thing(void);
~Thing(void);
int getValue(void) { return _value; }
void setValue(const int& x) { _value = x; }
private:
int _value;
};
問題:我可以構建的ContextNode
實例,像這樣。
Thing* thing = new Thing();
ContextNode<Thing,int,int>* cn = new ContextNode<Thing,int,int>(thing, &Thing::getValue, &Thing::setValue);
這適用於我的需要。我遇到問題,但重載的方法。假設我寫了:
class Thing {
public:
Thing(void);
~Thing(void);
int value(void) { return _value; }
void value(const int& x) { _value = x; }
private:
int _value;
};
Thing* thing = new Thing();
ContextNode<Thing,int,int>* cn = new ContextNode<Thing,int,int>(thing, &Thing::value, &Thing::value);
這未能鏈接。我相信這個問題是鏈接器僅嘗試基於名稱的解析,因此我看到了<unresolved overloaded function type>
錯誤。
我的問題:是否有一些語法糖來顯式指定我指的幾個重載方法中的哪一個?我無法想象這樣一個愚蠢的怪癖會打破這樣一個優雅的解決方案。我在網上找不到任何東西,也沒有找到C++常見問題解答,也沒有關於這個話題的SO。
什麼是修復程序,或者我洗過?
好主意,但是'錯誤:沒有上下文類型信息的重載函數的地址' – 2010-10-16 02:09:17
哦,具體來說,這個錯誤來自於getter的轉換,而不是setter。 – 2010-10-16 02:28:25
Bah - 我的錯。我有一些常量聲明是錯誤的。謝謝! – 2010-10-16 02:34:58