【问题标题】:map.insert: "Invalid arguments" error with pair<enum,vector<*>>map.insert:pair<enum,vector<*>> 出现“无效参数”错误
【发布时间】:2017-04-28 09:42:11
【问题描述】:

编辑 4:好的,我将问题缩小到一个小的 cpp 文件,给出了每个人都可以重现的相同错误消息:

#include<vector>
#include<map>
using namespace std;

namespace{enum class Colors{Black,White};}

class dummyClass{};

class MapTest
{
    vector<dummyClass*> Dummys;
    map<Colors,vector<dummyClass*>> dummyMap;
public:
    MapTest()
    {
        Dummys.push_back(new dummyClass);
        Dummys.push_back(new dummyClass);
        dummyMap.insert(make_pair(Colors::White,Dummys));
    }
    ~MapTest()
    {
        for(unsigned int i=0;i<Dummys.size();++i)
        {
            delete Dummys[i];
        }
        Dummys.clear();
        dummyMap.clear();
    }

};

int main()
{
    MapTest m;
}

编辑 3:万一有人想知道。我删除了析构函数中的所有指针。因为我认为它与问题无关,所以没有发布它

编辑 2:在 .cpp 文件中添加了更多详细信息。 “Bauer”和“Position”类不应该有任何相关性。

我的头文件match.h:

#include <map>
#include"Bauer.h"
class Match
{
    int counter{0};
    std::vector<Figuren*> allFiguresWhite;
    std::vector<Figuren*> allFiguresBlack;
    std::map<Farben,std::vector<Figuren*>> samePlayerFig;
    std::map<Farben,std::vector<Figuren*>> otherPlayerFig;
   //more code...
}

“Farben”只是一个枚举:enum class Farben{black, white}; 在我的 match.cpp 中,我尝试将一个 par 插入到地图中:

#include "match.h"
using namespace std;

Match::Match()
{
    cout<<"Let the games begin\n";
    InitGame();
}

void Match::InitGame()
{
    for(int i=1;i<9;i++)
    {
        allFiguresWhite.push_back(new Bauer(Position{2,i},Farben::white, counter++));
        allFiguresBlack.push_back(new Bauer(Position{7,i},Farben::black,counter++));
    }
    samePlayerFig.insert(make_pair(Farben::white,allFiguresWhite));
    //more code...
}

不幸的是,我在此行中收到编译器错误“无效参数”,其中“插入”蜂鸣以红色下划线显示。 知道我做错了什么吗?

编辑: 这是完整的错误信息

无效参数的候选者是: 标准::对>>>,布尔> 插入(const std::pair>> &) std::pair>>>,bool> 插入(#10000 &&) 无效插入(std::initializer_list>>>) std::_Rb_tree_iterator>>> 插入(std::_Rb_tree_const_iterator>>>, 常量 std::pair>> &) std::_Rb_tree_iterator>>> 插入(std::_Rb_tree_const_iterator>>>, #10000 &&) void insert(#10000, #10000) ' match.cpp /chessGame 第 17 行语义 错误

【问题讨论】:

  • 编译它并将真正的错误信息粘贴到问题中。 (不要依赖 IntelliSense 来判断真相;它提供的猜测比事实更合理。)
  • 不能用你给我们的东西重现,给我们看看你称之为的真实代码。
  • @ChVKishore:谢谢链接,但由于我没有使用原始数组,它不应该是错误的原因,对吧?据我了解,枚举的底层类型是 int。
  • 顺便说一句,您的vector 和您的new 存在史诗般的内存泄漏。 C++ 需要手动释放内存

标签: c++ dictionary vector enums


【解决方案1】:

好的,这似乎是 Eclipse 特定的错误。我使用“g++ main.cpp -o main”从命令行编译了测试程序,它工作正常。会调查它为什么它在eclipse上不起作用。到目前为止,我已经编译了很多项目,并且从未遇到任何编译器问题。

【讨论】:

  • 我对每个地图星座都有问题(, , ...)。找到让 eclipse 重新构建和运行项目的方法了吗?
  • 我找到了解决问题的方法。我的 Eclipse 接受“map[key] = value;”。
猜你喜欢
  • 2014-09-17
  • 2018-12-04
  • 2016-08-18
  • 2018-07-05
  • 2020-06-30
  • 2012-08-28
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
相关资源
最近更新 更多