我正在使用 ROOT frameowrk,我想写一个类,它有一个 TMultiGraph
成员。我正在尝试为我的类(class)编写和赋值运算符,但由于编译问题我失败了。 TMultiGraph
类将其赋值运算符保持为 protected
。
我类(class)的标题:
#include "../include/clipper.hpp"
#include "TMultiGraph.h"
#include "TColor.h"
#include "RtypesCore.h"
using namespace ClipperLib;
class ClipperDraw : protected TMultiGraph {
public:
ClipperDraw() {}
ClipperDraw& operator=(const ClipperDraw &c);
private:
TMultiGraph mg;
};
.cpp
是:
ClipperDraw& ClipperDraw::operator=(const ClipperDraw &c)
{
mg = c.mg;
return *this;
}
编译时我收到这条消息:
g++ -fPIC -Wall `root-config --cflags` -I./include -O2 -c -o obj/ClipperDraw.o src/ClipperDraw.cpp
In file included from src/../include/ClipperDraw.h:12:0,
from src/ClipperDraw.cpp:8:
/home/user/anaconda3/envs/deepjetLinux3/include/TMultiGraph.h: In member function ‘ClipperDraw& ClipperDraw::operator=(const ClipperDraw&)’:
/home/user/anaconda3/envs/deepjetLinux3/include/TMultiGraph.h:47:17: error: ‘TMultiGraph& TMultiGraph::operator=(const TMultiGraph&)’ is protected
TMultiGraph& operator=(const TMultiGraph&);
^
src/ClipperDraw.cpp:26:5: error: within this context
mg = c.mg;
^
Makefile:19: recipe for target 'obj/ClipperDraw.o' failed
make: *** [obj/ClipperDraw.o] Error 1
最佳答案
TMultiGraph
的复制构造函数和复制赋值运算符都被标记为 protected 。这意味着您不能将一个 TMultiGraph
分配给另一个 TMultiGraph
。继承不会帮助您,因为它不会改变这一事实。
从 TMultiGraph
继承的是允许创建您自己的可以复制的图形类。看起来像
class MyMultiGraph : public TMultiGraph {
//...
public:
MyMultiGraph& operator =(const MyMultiGraph& rhs)
{
TMultiGraph::operator=(rhs);
// assign MyMultiGraph member here
}
};
关于ROOT 类的 C++ 赋值运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46054527/