Static map initializer function error

[亡魂溺海] 提交于 2020-02-25 07:25:48

问题


I get the following base error:

1>c:\program files\microsoft visual studio 10.0\vc\include\utility(163): error C2436: 'second' : member function or nested class in constructor initializer list

As well as a lot of sub-errors there - I have no idea at all where to look or what goes wrong. (I know what functions it is about, but I'm staring myself blind on why it doesn't work)

The header part:

typedef void *DuplicateFn(pTree&, const pTree&); 
enum DuplicateTy {
    SKIP,
    OVERWRITE,
    ASK
};
typedef std::map<DuplicateTy, DuplicateFn> DuplicateMapTy;

static const DuplicateMapTy DuplicateFns;
static DuplicateMapTy DuplicateFns_INIT();

detail namespace:

namespace detail {
    void OverWriteFn(GMProject::pTree& tOut, const GMProject::pTree& tIn);
    void AskFn(GMProject::pTree&  tOut, const GMProject::pTree& tIn);
}

The source part:

GMProject::DuplicateMapTy GMProject::DuplicateFns_INIT() {
    DuplicateMapTy tmp;
    auto p(std::make_pair(GMProject::OVERWRITE, &detail::OverWriteFn));
    tmp.insert(p); //offending line
    return tmp;
}
const GMProject::DuplicateMapTy GMProject::DuplicateFns(GMProject::DuplicateFns_INIT());

As said I'm staring myself blind on this, why can't I insert that pair into the map? I'm simply inserting a function pointer & an enum?


回答1:


I might be wrong, but I don't like the line:

auto p(std::make_pair(GMProject::OVERWRITE, &detail::OverWriteFn));

Are you using VS 2010? You can hover the variable name (p) and see which type auto has deduced.

Also, have you tried:

 tmp.insert(std::make_pair(GMProject::OVERWRITE, &detail::OverWriteFn));

Or

tmp.insert(std::pair(GMProject::OVERWRITE, &detail::OverWriteFn));

?



来源:https://stackoverflow.com/questions/8256328/static-map-initializer-function-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!