【发布时间】:2019-12-25 15:39:41
【问题描述】:
我有这门课
class InventoryManagement
{
public:
typedef void(InventoryManagement::*Action)(void);
typedef void(InventoryManagement::*ActionWithReturn)(InventoryItem);
void Initialize(int inventoryQty, int maxQty, ActionWithReturn onAdd = nullptr , Action onMaxQtyReach = nullptr, Action onQtyReset = nullptr, Action onSideChnage = nullptr);
Action getStrategyOnMaxQtyReach();
Action getStrategyOnQtyReset();
Action getStrategyOnSideChange();
ActionWithReturn getOnAdd();
private:
// Private Functions Data members
ActionWithReturn m_onAdd;
Action m_OnSideChange;
Action m_OnQtyReset;
Action m_OnMaxQtyReach;
// Default Function
void OnAddDefault(InventoryItem item) { }
void OnMaxQtyReachDefault() { }
void OnQtyResetDefault() { }
void OnSideChangeDefault() { }
};
函数Initialize 设置Action 类型的数据成员。
我创建了注意的默认操作,我想将它们作为默认参数传递。如果用户没有向Initialize 发送任何函数指针,那么这些函数将被设置。
函数的签名是:
void Initialize(int inventoryQty, int maxQty, ActionWithReturn onAdd = nullptr , Action onMaxQtyReach = nullptr, Action onQtyReset = nullptr, Action onSideChnage = nullptr);
我试过了
void Initialize(int inventoryQty, int maxQty, ActionWithReturn onAdd = InventoryManagement::OnAddDefault , Action onMaxQtyReach = nullptr, Action onQtyReset = nullptr, Action onSideChnage = nullptr);
而不是nullptr,但编译器发出“调用没有对象参数的非静态成员函数”。
【问题讨论】:
-
function Initialize then thosw function will set.wat?InventoryManagement::*OnAddDefaultInventoryManagement和OnAddDefault是什么?提供minimal reproducible example。 -
InventoryManagement::OnAddDefault可以是static吗? -
@yaodav 请提供最小的完整代码。
-
错误的意思就是它所说的:您正在尝试调用没有对象的非静态方法。但是,您没有发布的错误消息的实际来源。请提供minimal reproducible example
-
很高兴您回答并按照我们的要求行事。但是,您需要 edit 您的问题以包含信息。评论用于要求澄清,问题必须包含所有相关信息。
标签: c++