视觉C++将其视为常量,而事实并非如此

visual C++ treating this as a const when it isn't

本文关键字:事实 并非如此 常量 C++ 视觉      更新时间:2023-10-16

我正在尝试使用Visual Studio 2010编译一些C++代码,但收到以下错误:

错误 C2664: 'molder::Mold::set_piece_maker' : 无法将参数 1 从 'piece_maker::P iece_Maker *const ' 转换为 'piecemaker::P iece_Maker *'

该错误指的是管理两个类之间相互引用的两个镜像函数:

void Piece_Maker::set_mold(molder::Mold* value, void* origin) {
    if (this->mold == value)
        return;
    this->mold = value;
    this->mold->set_piece_maker(this, this); // This is the line with the error
}

和:

void Mold::set_piece_maker(piecemaker::Piece_Maker* value, void* origin) {
    if (this->piece_maker == value)
        return;
    this->piece_maker = value;
    this->piece_maker->set_mold(this, this);
}
  • 我没有在任何地方对这些类中的任何一个使用 const。
  • 该错误仅发生在 mold->set_piece_maker() 行上,而镜像的 piece_maker->set_mold() 行编译得很好。
  • 编译器声称"this"
  • 是const,但没有标记我对其mod属性的修改,也没有将我传递的"this"标记为源参数。
  • 当我实际制作 set_mold() const 时,编译器会引发错误,以尝试修改该函数中的模具并尝试将"this"作为原点传递。

编译器引发该错误会发生什么?

问题不在于const,这是一个顶级const,无论如何都被忽略了。 看看垂直排列的两种类型:

piece_maker::Piece_Maker *const
piecemaker::Piece_Maker *