无法在c ++中初始化方法指针的映射

时间:2013-03-20 01:03:57

标签: c++ map visual-studio-2012

我有以下功能:

//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:'&amp;' :绑定成员函数表达式的非法操作。

当我定义映射时,我不知道我到底做错了什么。有什么想法吗?

修改

//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;
}

3 个答案:

答案 0 :(得分:0)

您无法在标准C ++中执行此操作。出于原因看看here&amp; here

修改 而不是将“Command *(*)()”存储到操作中,为什么不改变“操作”值类型来存储“Command *”?或者也许重新审视低级设计?

答案 1 :(得分:0)

您的代码存在一些问题。主要关注成员函数的使用而不是全局函数。您尝试从对象中获取成员函数,您应该从类本身获取它们。 (即代替&fact.createAdditionCommand你需要&CommandFactory::createAdditionCommand。)但这导致一个未绑定的成员函数,这意味着你需要使用(fact.*fn)()来调用它 - 即使用CommandFactory类的对象。这并不理想 - 而不是你正在寻找的东西。您正在寻找绑定成员函数。在非C ++ - 11应用程序中使用这些是可能的,但很难看。 您可以使用boost库来帮助实现这一点(除了stdboost样式更改之外,下面的代码几乎没有变化。)如果你正在使用C ++ -11然后你可以使用C ++ - 11函数对象。

以下是源自您的完整示例:

#include <vector>
#include <map>
#include <functional>
#include <string>

struct Command {};
struct Subtract : Command {};
struct Add : Command {};

class CommandFactory
{
  public:

    virtual Subtract * createSubtractionCommand() = 0;
    virtual Add * createAdditionCommand() = 0;
};

class StackCommandFactory : public CommandFactory
{
  public:

    virtual Subtract * createSubtractionCommand(void);
    virtual Add * createAdditionCommand(void);

    Subtract * sub;
    Add * add;
};

Subtract * StackCommandFactory::createSubtractionCommand(void) { return sub; }
Add * StackCommandFactory::createAdditionCommand(void) { return add; }

class Parser
{
  public:
    Parser (CommandFactory & fact);

    std::map<std::string, std::function<Command*()> > operations; 
};

Parser::Parser(CommandFactory & fact)
{
  operations["+"] = std::bind(&CommandFactory::createAdditionCommand, &fact);
  operations["-"] = std::bind(&CommandFactory::createSubtractionCommand, &fact);
}

#include <iostream>
int main()
{
  Add add;
  Subtract sub;

  StackCommandFactory command_factory;
  command_factory.add = &add;
  command_factory.sub= &sub;
  Parser parser(command_factory);

  std::cout<<"&add = "<<&add<<std::endl;
  std::cout<<"Add = " <<  parser.operations["+"]() <<std::endl;
  std::cout<<"&sub = "<<&sub<<std::endl;
  std::cout<<"Sub = " <<  parser.operations["-"]() <<std::endl;

  return 0;
}

我得到了输出

&add = 0x7fff58d538d8
Add = 0x7fff58d538d8
&sub = 0x7fff58d538d0
Sub = 0x7fff58d538d0

显示通过解析器返回的Add对象与存储在CommandFactory中的对象相同。 (对于Subtract对象也一样)

答案 2 :(得分:-1)

抱歉试试

操作[&#34; *&#34;] = fact.createMultiplyCommand;

操作[&#34; *&#34;] = fact-&gt; createMultiplyCommand;