C++ 非局部函数调用局部类型 432 void RoleExternModel::getShareConfig(SourceList&SourceList,std::vector&packs,ShopModel&ShopModel) 433 { 434 std::copy(Packs,Packs+SHARE_PRIZE,std::insert_迭代器(Packs,Packs.begin()); 435结构乐趣{ 436个std::向量对; 437 void运算符()(UInt16类型){ 438对。推回(标准::制作对(类型,配置::包)); 439 } 440 }; 441乐趣(); 442标准::每个(包,包+分享奖,乐趣); 443名shopModel shopModel(趣味双人); 444 shopModel.load(源列表); 445 }
为什么编译器说: 错误:非本地函数 'Pet::RoleExternModel::getShareConfig(Pet::SourceList&, 标准::向量>&, Pet::ShopModel&):Fun Pet::Fun()使用本地类型 'Pet::RoleExternModel::getShareConfig(Pet::SourceList&, 标准::向量>&, 宠物::商店模型&):乐趣C++ 非局部函数调用局部类型 432 void RoleExternModel::getShareConfig(SourceList&SourceList,std::vector&packs,ShopModel&ShopModel) 433 { 434 std::copy(Packs,Packs+SHARE_PRIZE,std::insert_迭代器(Packs,Packs.begin()); 435结构乐趣{ 436个std::向量对; 437 void运算符()(UInt16类型){ 438对。推回(标准::制作对(类型,配置::包)); 439 } 440 }; 441乐趣(); 442标准::每个(包,包+分享奖,乐趣); 443名shopModel shopModel(趣味双人); 444 shopModel.load(源列表); 445 },c++,C++,为什么编译器说: 错误:非本地函数 'Pet::RoleExternModel::getShareConfig(Pet::SourceList&, 标准::向量>&, Pet::ShopModel&):Fun Pet::Fun()使用本地类型 'Pet::RoleExternModel::getShareConfig(Pet::SourceList&, 标准::向量>&, 宠物::商店模型&):乐趣 如何使其成为legel?这一行是问题所在: 432 void RoleExtern
如何使其成为legel?这一行是问题所在:
432 void RoleExternModel::getShareConfig(SourceList& sourceList, std::vector<UInt16>& packs, ShopModel& shopModel)
433 {
434 std::copy(Packs, Packs + SHARE_PRIZE, std::insert_iterator<std::vector<UInt16> >(packs, packs.begin()));
435 struct Fun {
436 std::vector<std::pair<UInt16, UInt8> > pairs;
437 void operator () (UInt16 type) {
438 pairs.push_back(std::make_pair<UInt16, UInt8>(type, Configuration::PACKS));
439 }
440 };
441 Fun fun();
442 std::for_each(Packs, Packs + SHARE_PRIZE, fun);
443 shopModel shopModel(fun.pairs);
444 shopModel.load(sourceList);
445 }
它声明了一个函数名fun
,该函数不带任何参数,并返回一个fun
对象
这就是所谓的
去掉括号,您将声明一个名为
fun
的对象。其他答案在编译器错误方面在技术上是正确的,但忽略了一个微妙的问题:fun
使用不正确。它似乎是一个局部变量,用于在fun.pairs
中收集结果。但是,std::for_each
可以复制fun
,然后fun.pairs
不会更新
正确的解决方案:
Fun-Fun=std::for_each(Packs,Packs+SHARE_-PRIZE,Fun())代码>我们需要一个完整的自包含测试用例,希望能有所帮助。Fun()代码>或乐趣代码>,我怀疑!!!乐趣一个问题。。。如果解决这个问题没有帮助,那么我们需要看到更多。。比如,背包?包装是什么类型的?我看到“包”。。
Fun fun();