我有如下功能:
//Parser.cpp
//...
Parser::Parser(CommandFactory & fact,
ArrayList<std::string> & expression)
{
operations["*"] = &fact.createMultiplyCommand; //error C2276
operations["/"] = &fact.createAdditionCommand; //error C2276
operations["+"] = &fact.createAdditionCommand; //error C2276
operations["%"] = &fact.createModulusCommand; //error C2276
operations["-"] = &fact.createSubtractionCommand; //error C2276
infixToPostfix(fact,expression);
}
//...
//Parser.h
//...
Parser (CommandFactory & fact,
ArrayList<std::string> & expression);
Stack<Command*> tempCommandStack;
ArrayList<Command*> postfix;
/// Destructor.
~Parser (void);
private:
//syntax is: <return_type> (*)()
//syntax for a class method is: <return_type> (<class_name> *)();
//syntax with a typedef is: typedef <return_type> (<class_name> * <typedef_name>)();
// typedef <return_type> (*<typedef_name>)()
std::map<std::string, Command * (*)()> operations;
std::map<std::string,int> precedence;
//...
知道这CommandFactory
是一个抽象类也可能会有所帮助(传入的是具体的)
我收到的错误是 C2276: '&' : 对绑定成员函数表达式的非法操作。
当我定义映射时,我不知道我在做什么错。有任何想法吗?
编辑:
//CommandFactory.h
#ifndef _COMMANDFACTORY_H_
#define _COMMANDFACTORY_H_
#include "Subtract.h"
#include "Add.h"
#include "Divide.h"
#include "Multiply.h"
#include "Modulus.h"
#include "Negation.h"
class CommandFactory
{
public:
virtual Subtract * createSubtractionCommand() = 0;
virtual Add * createAdditionCommand() = 0;
virtual Divide * createDivisionCommand() = 0;
virtual Multiply * createMultiplyCommand() = 0;
virtual Modulus * createModulusCommand() = 0;
virtual Negation * createNegationCommand() = 0;
~CommandFactory(void);
};
#endif
//StackCommandFactory.h
#ifndef _STACKCOMMANDFACTORY_H_
#define _STACKCOMMANDFACTORY_H_
#include "Add.h"
#include "Subtract.h"
#include "Divide.h"
#include "Multiply.h"
#include "Modulus.h"
#include "CommandFactory.h"
class StackCommandFactory : public CommandFactory
{
public:
virtual Subtract * createSubtractionCommand(void);
virtual Add * createAdditionCommand(void);
virtual Divide * createDivisionCommand(void);
virtual Multiply * createMultiplyCommand(void);
virtual Modulus * createModulusCommand(void);
virtual Negation * createNegationCommand(void);
protected:
Subtract * sub;
Add * add;
Divide * div;
Multiply * mul;
Modulus * mod;
Negation * neg;
};
#endif // !defined _STACKCOMMANDFACTORY_H_
//StackCommandFactory.cpp
#include "StackCommandFactory.h"
Subtract * StackCommandFactory::createSubtractionCommand(void)
{
return sub;
}
Add * StackCommandFactory::createAdditionCommand(void)
{
return add;
}
Divide * StackCommandFactory::createDivisionCommand(void)
{
return div;
}
Multiply * StackCommandFactory::createMultiplyCommand(void)
{
return mul;
}
Modulus * StackCommandFactory::createModulusCommand(void)
{
return mod;
}
Negation * StackCommandFactory::createNegationCommand(void)
{
return neg;
}