CLI中报错无法访问私有成员,但是我的属性明明是public

波霸奶茶不要奶茶 2020-05-11 09:22:20
求解,我写了个CLI项目程序,调用了c++代码,会报错无法访问私有成员,错误信息如下:
error C2248: “TopoShapeGroup::WireGroup”: 无法访问 private 成员(在“TopoShapeGroup”类中声明)

我的TopoShapeGroup.h头文件内容如下:
public ref class TopoShapeGroup
{
public:
BRepBuilderAPI_MakeWire *WireGroup;//BRepBuilderAPI_MakeWire是一个c++类,我把它作为类成员

public:
//拷贝构造函数

TopoShapeGroup(const TopoShapeGroup^ c)
{
WireGroup=c->WireGroup;
}


TopoShapeGroup^ TopoShapeGroup::operator=(const TopoShapeGroup ^RightSides)
{
this->WireGroup=RightSides->WireGroup;
return this;
}
TopoShapeGroup();
void Add(Circ^ c);
void Add(Lin^ l);
};

使用的过程如下:
void View(TopoShapeGroup^ profile,Lin^ path)
{
BRepBuilderAPI_MakeWire *WireGroup=profile->WireGroup;//这行报错,说是“TopoShapeGroup::WireGroup”: 无法访问 private 成员(在“TopoShapeGroup”类中声明),我按着网上的解决方案在TopoShapeGroup类中添加拷贝构造函数和重载=运算符都没有用,还是报错,求解答
.....
}
...全文
374 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tobybo 2020-05-15
  • 打赏
  • 举报
回复
引用 11 楼 bo_self_effacing 的回复:
在C++/CLI中,不能重写默认构造函数。默认构造函数将所有的值类型数据成员初始化为0,将引用类型(句柄)初始化为nullptr。同样,也不能重载复制构造函数和赋值操作符。默认的复制操作是将每一个数据成员进行复制,对象间的赋值也是如此。
https://www.cnblogs.com/herenzhiming/articles/6692171.html 来自这里哈,原来^ 是c++/cli 的符号哈,孤陋寡闻啦。
tobybo 2020-05-15
  • 打赏
  • 举报
回复
在C++/CLI中,不能重写默认构造函数。默认构造函数将所有的值类型数据成员初始化为0,将引用类型(句柄)初始化为nullptr。同样,也不能重载复制构造函数和赋值操作符。默认的复制操作是将每一个数据成员进行复制,对象间的赋值也是如此。
tobybo 2020-05-15
  • 打赏
  • 举报
回复
把你参数为 指针的 拷贝构造函数和 赋值函数 都删了把。。感觉会有反效果。 指针传递就是传个地址而已,不会触发默认的构造函数的。
真相重于对错 2020-05-15
  • 打赏
  • 举报
回复
考虑 一下,一个ref class 放一个非托管的class,会出现什么情况,建议看一下msdn中关于托管与非托管的交互问题。
tobybo 2020-05-15
  • 打赏
  • 举报
回复
好吧 你都用 c->WireGroup 了, 应该是 * 号,当我没说。
tobybo 2020-05-15
  • 打赏
  • 举报
回复
重载 = 肯定没啥用, 重载拷贝构造函数 你这写法和没重载一个样 , void View(TopoShapeGroup^ profile,Lin^ path) 你这里这个 ^ 是啥 是 & 吗 如果是 & 号 那就不会调用拷贝构造函数,就不用看下面这个改法了。如果不是 &, 你可以试试改成 &,或者试下以下改法。 你把TopoShapeGroup 的拷贝构造函数重载改一下: TopoShapeGroup(const TopoShapeGroup^ c) { WireGroup=new BRepBuilderAPI_MakeWire(*c->WireGroup); //注意 在BRepBuilderAPI_MakeWire 这里面再重载一个拷贝构造函数 //或者 WireGroup = new BRepBuilderAPI_MakeWire(); *WireGroup = *c->WireGroup; //注意 在BRepBuilderAPI_MakeWire 这里面再重载一下 = 运算符 }
  • 打赏
  • 举报
回复
引用 5 楼 千梦一生 的回复:
改为int* 测试
我有试过在CLI中定义c++成员,是可以成功使用的,但就是这个不成功,不知道为什么
千梦一生 2020-05-12
  • 打赏
  • 举报
回复
改为int* 测试
千梦一生 2020-05-12
  • 打赏
  • 举报
回复
WireGroup 类型改为int 测试。
  • 打赏
  • 举报
回复
引用 2 楼 Simple-Soft 的回复:
完整代码有吗?
你好,我提取了关键段落,主要是TopoShapeGroup.h和调用它的方法,就是在调用方法里报错在报错。我现在贴一下BRepBuilderAPI_MakeWire的头文件。 // Created on: 1993-07-08 // Created by: Remi LEQUETTE // Copyright (c) 1993-1999 Matra Datavision // Copyright (c) 1999-2014 OPEN CASCADE SAS // // This file is part of Open CASCADE Technology software library. // // This library is free software; you can redistribute it and/or modify it under // the terms of the GNU Lesser General Public License version 2.1 as published // by the Free Software Foundation, with special exception defined in the file // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT // distribution for complete text of the license and disclaimer of any warranty. // // Alternatively, this file may be used under the terms of Open CASCADE // commercial license or contractual agreement. #ifndef _BRepBuilderAPI_MakeWire_HeaderFile #define _BRepBuilderAPI_MakeWire_HeaderFile #include <Standard.hxx> #include <Standard_DefineAlloc.hxx> #include <Standard_Handle.hxx> #include <BRepLib_MakeWire.hxx> #include <BRepBuilderAPI_MakeShape.hxx> #include <TopTools_ListOfShape.hxx> #include <Standard_Boolean.hxx> #include <BRepBuilderAPI_WireError.hxx> class StdFail_NotDone; class TopoDS_Edge; class TopoDS_Wire; class TopoDS_Vertex; //! Describes functions to build wires from edges. A wire can //! be built from any number of edges. //! To build a wire you first initialize the construction, then //! add edges in sequence. An unlimited number of edges //! can be added. The initialization of construction is done with: //! - no edge (an empty wire), or //! - edges of an existing wire, or //! - up to four connectable edges. //! In order to be added to a wire under construction, an //! edge (unless it is the first one) must satisfy the following //! condition: one of its vertices must be geometrically //! coincident with one of the vertices of the wire (provided //! that the highest tolerance factor is assigned to the two //! vertices). It could also be the same vertex. //! - The given edge is shared by the wire if it contains: //! - two vertices, identical to two vertices of the wire //! under construction (a general case of the wire closure), or //! - one vertex, identical to a vertex of the wire under //! construction; the other vertex not being //! geometrically coincident with another vertex of the wire. //! - In other cases, when one of the vertices of the edge //! is simply geometrically coincident with a vertex of the //! wire under construction (provided that the highest //! tolerance factor is assigned to the two vertices), the //! given edge is first copied and the coincident vertex is //! replaced in this new edge, by the coincident vertex of the wire. //! Note: it is possible to build non manifold wires using this construction tool. //! A MakeWire object provides a framework for: //! - initializing the construction of a wire, //! - adding edges to the wire under construction, and //! - consulting the result. class BRepBuilderAPI_MakeWire : public BRepBuilderAPI_MakeShape { public: DEFINE_STANDARD_ALLOC //! Constructs an empty wire framework, to which edges //! are added using the Add function. //! As soon as the wire contains one edge, it can return //! with the use of the function Wire. //! Warning //! The function Error will return //! BRepBuilderAPI_EmptyWire if it is called before at //! least one edge is added to the wire under construction. Standard_EXPORT BRepBuilderAPI_MakeWire(); //! Make a Wire from an edge. Standard_EXPORT BRepBuilderAPI_MakeWire(const TopoDS_Edge& E); //! Make a Wire from two edges. Standard_EXPORT BRepBuilderAPI_MakeWire(const TopoDS_Edge& E1, const TopoDS_Edge& E2); //! Make a Wire from three edges. Standard_EXPORT BRepBuilderAPI_MakeWire(const TopoDS_Edge& E1, const TopoDS_Edge& E2, const TopoDS_Edge& E3); //! Make a Wire from four edges. //! Constructs a wire //! - from the TopoDS_Wire W composed of the edge E, or //! - from edge E, or //! - from two edges E1 and E2, or //! - from three edges E1, E2 and E3, or //! - from four edges E1, E2, E3 and E4. //! Further edges can be added using the function Add. //! Given edges are added in a sequence. Each of them //! must be connectable to the wire under construction, //! and so must satisfy the following condition (unless it is //! the first edge of the wire): one of its vertices must be //! geometrically coincident with one of the vertices of the //! wire (provided that the highest tolerance factor is //! assigned to the two vertices). It could also be the same vertex. //! Warning //! If an edge is not connectable to the wire under //! construction it is not added. The function Error will //! return BRepBuilderAPI_DisconnectedWire, the //! function IsDone will return false and the function Wire //! will raise an error, until a new connectable edge is added. Standard_EXPORT BRepBuilderAPI_MakeWire(const TopoDS_Edge& E1, const TopoDS_Edge& E2, const TopoDS_Edge& E3, const TopoDS_Edge& E4); //! Make a Wire from a Wire. Usefull for adding later. Standard_EXPORT BRepBuilderAPI_MakeWire(const TopoDS_Wire& W); //! Add an edge to a wire. Standard_EXPORT BRepBuilderAPI_MakeWire(const TopoDS_Wire& W, const TopoDS_Edge& E); //! Adds the edge E to the wire under construction. //! E must be connectable to the wire under construction, and, unless it //! is the first edge of the wire, must satisfy the following //! condition: one of its vertices must be geometrically coincident //! with one of the vertices of the wire (provided that the highest //! tolerance factor is assigned to the two vertices). It could also //! be the same vertex. //! Warning //! If E is not connectable to the wire under construction it is not //! added. The function Error will return //! BRepBuilderAPI_DisconnectedWire, the function IsDone will return //! false and the function Wire will raise an error, until a new //! connectable edge is added. Standard_EXPORT void Add (const TopoDS_Edge& E); //! Add the edges of <W> to the current wire. Standard_EXPORT void Add (const TopoDS_Wire& W); //! Adds the edges of <L> to the current wire. The //! edges are not to be consecutive. But they are to //! be all connected geometrically or topologically. //! If some of them are not connected the Status give //! DisconnectedWire but the "Maker" is Done() and you //! can get the partial result. (ie connected to the //! first edgeof the list <L>) Standard_EXPORT void Add (const TopTools_ListOfShape& L); //! Returns true if this algorithm contains a valid wire. //! IsDone returns false if: //! - there are no edges in the wire, or //! - the last edge which you tried to add was not connectable. Standard_EXPORT virtual Standard_Boolean IsDone() const Standard_OVERRIDE; //! Returns the construction status //! - BRepBuilderAPI_WireDone if the wire is built, or //! - another value of the BRepBuilderAPI_WireError //! enumeration indicating why the construction failed. Standard_EXPORT BRepBuilderAPI_WireError Error() const; //! Returns the constructed wire; or the part of the wire //! under construction already built. //! Exceptions StdFail_NotDone if a wire is not built. Standard_EXPORT const TopoDS_Wire& Wire(); Standard_EXPORT operator TopoDS_Wire(); //! Returns the last edge added to the wire under construction. //! Warning //! - This edge can be different from the original one (the //! argument of the function Add, for instance,) //! - A null edge is returned if there are no edges in the //! wire under construction, or if the last edge which you //! tried to add was not connectable.. Standard_EXPORT const TopoDS_Edge& Edge() const; //! Returns the last vertex of the last edge added to the //! wire under construction. //! Warning //! A null vertex is returned if there are no edges in the wire //! under construction, or if the last edge which you tried to //! add was not connectableR Standard_EXPORT const TopoDS_Vertex& Vertex() const; protected: private: BRepLib_MakeWire myMakeWire; }; #endif // _BRepBuilderAPI_MakeWire_HeaderFile
Simple-Soft 2020-05-11
  • 打赏
  • 举报
回复
完整代码有吗?
  • 打赏
  • 举报
回复
求助,我之前在CLI项目中引用过c++的类并没有报错,这次不知道为什么就报这个错,已经卡了好几天了,求解惑。

65,212

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧