error C2166: l-value specifies const object

本文介绍C++中mutable关键字的用途及示例。mutable允许在const成员函数中修改非静态数据成员,文中通过示例详细解释了如何使用mutable解决编译错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键字mutable是C++中一个不常用的关键字,他只能用于类的非静态和非常量数据成员
  我们知道一个对象的状态由该对象的非静态数据成员决定,所以随着数据成员的改变,
  对像的状态也会随之发生变化!

  如果一个类的成员函数被声明为const类型,表示该函数不会改变对象的状态,也就是
  该函数不会修改类的非静态数据成员.但是有些时候需要在该类函数中对类的数据成员
  进行赋值.这个时候就需要用到mutable关键字了

  例如:

   class Demo
  {
  public:
  Demo(){}
  ~Demo(){}
  public:
  bool getFlag() const
  {
  m_nAccess++;
  return m_bFlag;
  }
  private:
  int  m_nAccess;
  bool m_bFlag;
  };
  int main()
  {
  return 0;
  }
 


  编译上面的代码会出现 error C2166: l-value specifies const object的错误
  说明在const类型的函数中改变了类的非静态数据成员.

  这个时候需要使用mutable来修饰一下要在const成员函数中改变的非静态数据成员
  m_nAccess,代码如下:


   class Demo
  {
  public:
  Demo(){}
  ~Demo(){}
  public:
  bool getFlag() const
  {
  m_nAccess++;
  return m_bFlag;
  }
  private:
  mutable int  m_nAccess;
  bool m_bFlag;
  };
  int main()
  {
  return 0;
  }
 


  这样再重新编译的时候就不会出现错误了!


本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/gengxt2003/archive/2008/07/26/2715746.aspx

我正在学习NX二次开发,用的是Visual Studio 2019,ug版本是12.0,开发平台是c++,uf函数为主nxopen函数为辅。需要你辅助我开发,重点注意开发模版是c++,优先使用uf函数,要求你先搞清楚你提供给我的代码真实有效性,语句与标点符号的正确位置,避免出现ug12.0没有提供的接口成员出现!确保所有方法调用都符合 NX12 的 API 签名,深度思考下再回答,每次回答前先回顾下前面的所有对话避免问题重复出现,每行代码都用中文注解具体意思与作用。下面开始帮我解决问题://============================================================================== // WARNING!! This file is overwritten by the Block UI Styler while generating // the automation code. Any modifications to this file will be lost after // generating the code again. // // Filename: D:\NXopen\BaiduSyncdisk\studio\hkjm\ui\hkjm.cpp // // This file was generated by the NX Block UI Styler // Created by: MiCH-CEO // Version: NX 12 // Date: 08-06-2025 (Format: mm-dd-yyyy) // Time: 19:18 (Format: hh-mm) // //============================================================================== //============================================================================== // Purpose: This TEMPLATE file contains C++ source to guide you in the // construction of your Block application dialog. The generation of your // dialog file (.dlx extension) is the first step towards dialog construction // within NX. You must now create a NX Open application that // utilizes this file (.dlx). // // The information in this file provides you with the following: // // 1. Help on how to load and display your Block UI Styler dialog in NX // using APIs provided in NXOpen.BlockStyler namespace // 2. The empty callback methods (stubs) associated with your dialog items // have also been placed in this file. These empty methods have been // created simply to start you along with your coding requirements. // The method name, argument list and possible return values have already // been provided for you. //============================================================================== //------------------------------------------------------------------------------ //These includes are needed for the following template code //------------------------------------------------------------------------------ #include "hkjm.hpp" using namespace NXOpen; using namespace NXOpen::BlockStyler; //------------------------------------------------------------------------------ // Initialize static variables //------------------------------------------------------------------------------ Session *(hkjm::theSession) = NULL; UI *(hkjm::theUI) = NULL; //------------------------------------------------------------------------------ // Constructor for NX Styler class //------------------------------------------------------------------------------ hkjm::hkjm() { try { // Initialize the NX Open C++ API environment hkjm::theSession = NXOpen::Session::GetSession(); hkjm::theUI = UI::GetUI(); theDlxFileName = "hkjm.dlx"; theDialog = hkjm::theUI->CreateDialog(theDlxFileName); // Registration of callback functions theDialog->AddApplyHandler(make_callback(this, &hkjm::apply_cb)); theDialog->AddOkHandler(make_callback(this, &hkjm::ok_cb)); theDialog->AddUpdateHandler(make_callback(this, &hkjm::update_cb)); theDialog->AddInitializeHandler(make_callback(this, &hkjm::initialize_cb)); theDialog->AddDialogShownHandler(make_callback(this, &hkjm::dialogShown_cb)); } catch(exception& ex) { //---- Enter your exception handling code here ----- throw; } } //------------------------------------------------------------------------------ // Destructor for NX Styler class //------------------------------------------------------------------------------ hkjm::~hkjm() { if (theDialog != NULL) { delete theDialog; theDialog = NULL; } } //------------------------------- DIALOG LAUNCHING --------------------------------- // // Before invoking this application one needs to open any part/empty part in NX // because of the behavior of the blocks. // // Make sure the dlx file is in one of the following locations: // 1.) From where NX session is launched // 2.) $UGII_USER_DIR/application // 3.) For released applications, using UGII_CUSTOM_DIRECTORY_FILE is highly // recommended. This variable is set to a full directory path to a file // containing a list of root directories for all custom applications. // e.g., UGII_CUSTOM_DIRECTORY_FILE=$UGII_BASE_DIR\ugii\menus\custom_dirs.dat // // You can create the dialog using one of the following way: // // 1. USER EXIT // // 1) Create the Shared Library -- Refer "Block UI Styler programmer's guide" // 2) Invoke the Shared Library through File->Execute->NX Open menu. // //------------------------------------------------------------------------------ extern "C" DllExport void ufusr(char *param, int *retcod, int param_len) { hkjm *thehkjm = NULL; try { UF_initialize(); //开发的许可函数,初始化 thehkjm = new hkjm(); // The following method shows the dialog immediately thehkjm->Show(); } catch(exception& ex) { //---- Enter your exception handling code here ----- hkjm::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } if(thehkjm != NULL) { delete thehkjm; thehkjm = NULL; }UF_terminate(); //释放许可函数,结束化 } //------------------------------------------------------------------------------ // This method specifies how a shared image is unloaded from memory // within NX. This method gives you the capability to unload an // internal NX Open application or user exit from NX. Specify any // one of the three constants as a return value to determine the type // of unload to perform: // // // Immediately : unload the library as soon as the automation program has completed // Explicitly : unload the library from the "Unload Shared Image" dialog // AtTermination : unload the library when the NX session terminates // // // NOTE: A program which associates NX Open applications with the menubar // MUST NOT use this option since it will UNLOAD your NX Open application image // from the menubar. //------------------------------------------------------------------------------ extern "C" DllExport int ufusr_ask_unload() { //return (int)Session::LibraryUnloadOptionExplicitly; return (int)Session::LibraryUnloadOptionImmediately; //return (int)Session::LibraryUnloadOptionAtTermination; } //------------------------------------------------------------------------------ // Following method cleanup any housekeeping chores that may be needed. // This method is automatically called by NX. //------------------------------------------------------------------------------ extern "C" DllExport void ufusr_cleanup(void) { try { //---- Enter your callback code here ----- } catch(exception& ex) { //---- Enter your exception handling code here ----- hkjm::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } } int hkjm::Show() { try { theDialog->Show(); } catch(exception& ex) { //---- Enter your exception handling code here ----- hkjm::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } return 0; } //------------------------------------------------------------------------------ //---------------------Block UI Styler Callback Functions-------------------------- //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ //Callback Name: initialize_cb //------------------------------------------------------------------------------ void hkjm::initialize_cb() { try { group0 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group0")); hk2edge = dynamic_cast<NXOpen::BlockStyler::CurveCollector*>(theDialog->TopBlock()->FindBlock("hk2edge")); hk1edge = dynamic_cast<NXOpen::BlockStyler::CurveCollector*>(theDialog->TopBlock()->FindBlock("hk1edge")); hk1sd = dynamic_cast<NXOpen::BlockStyler::ExpressionBlock*>(theDialog->TopBlock()->FindBlock("hk1sd")); hk2sd = dynamic_cast<NXOpen::BlockStyler::ExpressionBlock*>(theDialog->TopBlock()->FindBlock("hk2sd")); group = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group")); hkdj = dynamic_cast<NXOpen::BlockStyler::ExpressionBlock*>(theDialog->TopBlock()->FindBlock("hkdj")); hk2jd = dynamic_cast<NXOpen::BlockStyler::ExpressionBlock*>(theDialog->TopBlock()->FindBlock("hk2jd")); hk1jd = dynamic_cast<NXOpen::BlockStyler::ExpressionBlock*>(theDialog->TopBlock()->FindBlock("hk1jd")); vector0 = dynamic_cast<NXOpen::BlockStyler::SpecifyVector*>(theDialog->TopBlock()->FindBlock("vector0")); bodySelect0 = dynamic_cast<NXOpen::BlockStyler::BodyCollector*>(theDialog->TopBlock()->FindBlock("bodySelect0")); color1 = dynamic_cast<NXOpen::BlockStyler::ObjectColorPicker*>(theDialog->TopBlock()->FindBlock("color1")); color2 = dynamic_cast<NXOpen::BlockStyler::ObjectColorPicker*>(theDialog->TopBlock()->FindBlock("color2")); } catch(exception& ex) { //---- Enter your exception handling code here ----- hkjm::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } } //------------------------------------------------------------------------------ //Callback Name: dialogShown_cb //This callback is executed just before the dialog launch. Thus any value set //here will take precedence and dialog will be launched showing that value. //------------------------------------------------------------------------------ void hkjm::dialogShown_cb() { try { //---- Enter your callback code here ----- } catch(exception& ex) { //---- Enter your exception handling code here ----- hkjm::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } } //------------------------------------------------------------------------------ //Callback Name: apply_cb //------------------------------------------------------------------------------ // 应用按钮回调函数 - 读取所有控件数据 //------------------------------------------------------------------------------ int hkjm::apply_cb() { int errorCode = 0; // 错误代码,0表示成功 // 声明变量存储所有控件值 std::vector<NXOpen::Curve*> sectionCurves1; std::vector<NXOpen::Curve*> sectionCurves2; double hk1sdValue = 0.0; double hk2sdValue = 0.0; double hkdjValue = 0.0; double hk1jdValue = 0.0; double hk2jdValue = 0.0; NXOpen::Vector3d directionVector(0.0, 0.0, 1.0); std::vector<NXOpen::Body*> selectedBodies; int colorIndex1 = 0; int colorIndex2 = 0; try { // 获取NX会话和工作部件 NXOpen::Session* theSession = NXOpen::Session::GetSession(); NXOpen::Part* workPart = theSession->Parts()->Work(); NXOpen::UI* theUI = NXOpen::UI::GetUI(); // ===== 1. 处理曲线选择器1的值 ===== if (hk1edge) { // 使用智能指针管理属性列表内存 std::unique_ptr<NXOpen::BlockStyler::PropertyList> props(hk1edge->GetProperties()); std::vector<NXOpen::TaggedObject*> selObjs = props->GetTaggedObjectVector("SelectedObjects"); for (NXOpen::TaggedObject* obj : selObjs) { if (NXOpen::Curve* curve = dynamic_cast<NXOpen::Curve*>(obj)) { sectionCurves1.push_back(curve); } } } // ===== 2. 处理曲线选择器2的值 ===== if (hk2edge) { std::unique_ptr<NXOpen::BlockStyler::PropertyList> props(hk2edge->GetProperties()); std::vector<NXOpen::TaggedObject*> selObjs = props->GetTaggedObjectVector("SelectedObjects"); for (NXOpen::TaggedObject* obj : selObjs) { if (NXOpen::Curve* curve = dynamic_cast<NXOpen::Curve*>(obj)) { sectionCurves2.push_back(curve); } } } // ===== 3. 获取hk1sd表达式值 ===== if (hk1sd) { std::unique_ptr<NXOpen::BlockStyler::PropertyList> props(hk1sd->GetProperties()); hk1sdValue = props->GetDouble("Value"); } // ===== 4. 获取hk2sd表达式值 ===== if (hk2sd) { std::unique_ptr<NXOpen::BlockStyler::PropertyList> props(hk2sd->GetProperties()); hk2sdValue = props->GetDouble("Value"); } // ===== 5. 获取hkdj表达式值 ===== if (hkdj) { std::unique_ptr<NXOpen::BlockStyler::PropertyList> props(hkdj->GetProperties()); hkdjValue = props->GetDouble("Value"); } // ===== 6. 获取hk1jd表达式值 ===== if (hk1jd) { std::unique_ptr<NXOpen::BlockStyler::PropertyList> props(hk1jd->GetProperties()); hk1jdValue = props->GetDouble("Value"); } // ===== 7. 获取hk2jd表达式值 ===== if (hk2jd) { std::unique_ptr<NXOpen::BlockStyler::PropertyList> props(hk2jd->GetProperties()); hk2jdValue = props->GetDouble("Value"); } // ===== 8. 获取矢量方向 ===== if (vector0) { std::unique_ptr<NXOpen::BlockStyler::PropertyList> props(vector0->GetProperties()); directionVector = props->GetVector("Vector"); } // ===== 9. 处理选中的实体 ===== if (bodySelect0) { std::unique_ptr<NXOpen::BlockStyler::PropertyList> props(bodySelect0->GetProperties()); std::vector<NXOpen::TaggedObject*> selObjs = props->GetTaggedObjectVector("SelectedObjects"); for (NXOpen::TaggedObject* obj : selObjs) { if (NXOpen::Body* body = dynamic_cast<NXOpen::Body*>(obj)) { selectedBodies.push_back(body); } } } // ===== 10. 获取颜色选择器1的颜色 ===== if (color1) { std::unique_ptr<NXOpen::BlockStyler::PropertyList> colorProps(color1->GetProperties()); std::vector<int> colors = colorProps->GetIntegerVector("Value"); if (!colors.empty()) colorIndex1 = colors[0]; } // ===== 11. 获取颜色选择器2的颜色 ===== if (color2) { std::unique_ptr<NXOpen::BlockStyler::PropertyList> colorProps(color2->GetProperties()); std::vector<int> colors = colorProps->GetIntegerVector("Value"); if (!colors.empty()) colorIndex2 = colors[0]; } // ===== 3. 创建拉伸特征 ===== if (!sectionCurves1.empty() && hk1sdValue != 0.0) { // 设置撤销标记 NXOpen::Session::UndoMarkId markId = theSession->SetUndoMark( NXOpen::Session::MarkVisibilityVisible, "拉伸操作"); // 创建拉伸构建器 NXOpen::Features::ExtrudeBuilder* extrudeBuilder = workPart->Features()->CreateExtrudeBuilder(nullptr); // 创建截面 NXOpen::Section* section = workPart->Sections()->CreateSection( 0.001, 0.001, 0.05); extrudeBuilder->SetSection(section); // 创建曲线规则 - 使用dumb规则 std::vector<NXOpen::SelectionIntentRule*> rules; if (!sectionCurves1.empty()) { // 转换为TaggedObject向量 std::vector<NXOpen::TaggedObject*> curveObjects; for (NXOpen::Curve* curve : sectionCurves1) { curveObjects.push_back(curve); } // 创建dumb曲线规则 NXOpen::SelectionIntentRule* curveRule = workPart->ScRuleFactory()->CreateRuleCurveDumb(curveObjects); rules.push_back(curveRule); } // 获取帮助点(曲线中点)- NX12兼容方法 NXOpen::Point3d helpPoint(0.0, 0.0, 0.0); if (!sectionCurves1.empty()) { double startParam = 0.0, endParam = 0.0; // 获取曲线的参数范围 sectionCurves1[0]->GetLimits(&startParam, &endParam); // 计算中点参数 double midParam = (startParam + endParam) / 2.0; // 评估曲线中点位置 NXOpen::Point3d point; sectionCurves1[0]->Evaluate(midParam, &point); helpPoint = point; } // 添加到截面 - 使用正确的API签名 section->AddToSection( rules, // 规则向量 sectionCurves1[0], // 种子曲线 NULL, // 起始连接对象(无) NULL, // 结束连接对象(无) helpPoint, // 帮助点 NXOpen::Section::ModeCreate // 创建模式 ); // 设置拉伸方向 NXOpen::Vector3d directionVec(0.0, 0.0, 1.0); // 默认Z轴 if (vector0) { std::unique_ptr<NXOpen::BlockStyler::PropertyList> vecProps(vector0->GetProperties()); directionVec = vecProps->GetVector("Vector"); } NXOpen::Direction* extrudeDirection = workPart->Directions()->CreateDirection( NXOpen::Point3d(0.0, 0.0, 0.0), directionVec, NXOpen::SmartObject::UpdateOptionWithinModeling ); extrudeBuilder->SetDirection(extrudeDirection); // 设置拉伸范围 NXOpen::GeometricUtilities::Limits* limits = extrudeBuilder->Limits(); limits->StartExtend()->Value()->SetRightHandSide("0"); limits->EndExtend()->Value()->SetRightHandSide( std::to_string(hk1sdValue).c_str()); // 设置布尔操作类型(创建新实体) extrudeBuilder->BooleanOperation()->SetType( NXOpen::GeometricUtilities::BooleanOperation::BooleanTypeCreate); // 执行拉伸操作 NXOpen::NXObject* featureNXObject = extrudeBuilder->Commit(); NXOpen::Features::Feature* feature = dynamic_cast<NXOpen::Features::Feature*>(featureNXObject); // 设置颜色 int colorIndex = 0; if (color1) { std::unique_ptr<NXOpen::BlockStyler::PropertyList> colorProps(color1->GetProperties()); std::vector<int> colors = colorProps->GetIntegerVector("Value"); if (!colors.empty()) colorIndex = colors[0]; } if (feature && colorIndex > 0) { // 使用GetEntities获取特征创建的所有实体 std::vector<NXOpen::TaggedObject*> entities; feature->GetEntities(entities); // 过滤出Body对象 for (NXOpen::TaggedObject* obj : entities) { if (NXOpen::Body* body = dynamic_cast<NXOpen::Body*>(obj)) { body->SetColor(colorIndex); break; // 只设置第一个体 } } } // 清理资源 extrudeBuilder->Destroy(); section->Destroy(); } } catch (std::exception& ex) { errorCode = 1; hkjm::theUI->NXMessageBox()->Show("错误", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } catch (...) { errorCode = 1; hkjm::theUI->NXMessageBox()->Show("错误", NXOpen::NXMessageBox::DialogTypeError, "未知错误"); } return errorCode; } //------------------------------------------------------------------------------ //Callback Name: update_cb //------------------------------------------------------------------------------ int hkjm::update_cb(NXOpen::BlockStyler::UIBlock* block) { try { if(block == hk2edge) { //---------Enter your code here----------- } else if(block == hk1edge) { //---------Enter your code here----------- } else if(block == hk1sd) { //---------Enter your code here----------- } else if(block == hk2sd) { //---------Enter your code here----------- } else if(block == hkdj) { //---------Enter your code here----------- } else if(block == hk2jd) { //---------Enter your code here----------- } else if(block == hk1jd) { //---------Enter your code here----------- } else if(block == vector0) { //---------Enter your code here----------- } else if(block == bodySelect0) { //---------Enter your code here----------- } else if(block == color1) { //---------Enter your code here----------- } else if(block == color2) { //---------Enter your code here----------- } } catch(exception& ex) { //---- Enter your exception handling code here ----- hkjm::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } return 0; } //------------------------------------------------------------------------------ //Callback Name: ok_cb //------------------------------------------------------------------------------ int hkjm::ok_cb() { int errorCode = 0; try { errorCode = apply_cb(); } catch(exception& ex) { //---- Enter your exception handling code here ----- errorCode = 1; hkjm::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } return errorCode; } //------------------------------------------------------------------------------ //Function Name: GetBlockProperties //Description: Returns the propertylist of the specified BlockID //------------------------------------------------------------------------------ PropertyList* hkjm::GetBlockProperties(const char *blockID) { return theDialog->GetBlockProperties(blockID); } 报错:错误(活动) E0135 class "NXOpen::Curve" 没有成员 "GetLimits" hkjm D:\NXopen\BaiduSyncdisk\studio\hkjm\hkjm.cpp 395 错误(活动) E0312 不存在用户定义的从 "std::vector<NXOpen::TaggedObject *, std::allocator<NXOpen::TaggedObject *>>" 到 "const std::vector<NXOpen::Curve *, std::allocator<NXOpen::Curve *>>" 的适当转换 hkjm D:\NXopen\BaiduSyncdisk\studio\hkjm\hkjm.cpp 386 错误(活动) E0140 函数调用中的参数太多 hkjm D:\NXopen\BaiduSyncdisk\studio\hkjm\hkjm.cpp 402 错误(活动) E0140 函数调用中的参数太多 hkjm D:\NXopen\BaiduSyncdisk\studio\hkjm\hkjm.cpp 457
最新发布
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值