我有一个包含类的头文件.在那个类中,我有一个像这样的函数:
class Definition
{
public:
int GetID()
{
return Id;
}
//Other methods/variables
private:
int Id;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试获取该ID时:
for (std::map<Definition, std::vector<bool> >::iterator mapit = DefUseMap.begin(); mapit != DefUseMap.end(); ++mapit, defIndex++)
{
stream << "Definition " << (*mapit).first.GetID() << " Def Use" << endl << "\t";
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
CFG.cc:1145:错误:将'const Definition'作为'int Definition :: GetID()'的'this'参数传递,丢弃限定符
是因为我在地图中使用了定义,而且我不允许在该映射定义上调用方法?有没有办法让ID变量出来?
提前致谢
声明getID()方法const:
int getId() const
{
return Id;
}
Run Code Online (Sandbox Code Playgroud)
然后可以通过const引用调用该方法,这是operator<<()传递的内容.