【发布时间】: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 来判断真相;它提供的猜测比事实更合理。)
-
不能用你给我们的东西重现,给我们看看你称之为的真实代码。
-
请参考这个:: (stackoverflow.com/questions/35452739/…)
-
@ChVKishore:谢谢链接,但由于我没有使用原始数组,它不应该是错误的原因,对吧?据我了解,枚举的底层类型是 int。
-
顺便说一句,您的
vector和您的new存在史诗般的内存泄漏。 C++ 需要手动释放内存
标签: c++ dictionary vector enums