我有一个类似的课程:
class Qtree
{
public:
Qtree();
Qtree(BMP img, int d);
private:
class QtreeNode
{
public:
QtreeNode* nwChild; // pointer to northwest child
QtreeNode* neChild; // pointer to northeast child
QtreeNode* swChild; // pointer to southwest child
QtreeNode* seChild; // pointer to southeast child
RGBApixel element; // the pixel stored as this node's "data"
QtreeNode();
QuadtreeNode copy(QuadtreeNode & n);
};
因此问题在于复制方法。它生成给定节点的副本并返回它。
QtreeNode Qtree::QtreeNode::copy(QtreeNode & n) {
QtreeNode *newNode;
//....
return newNode;
}
然后我从我的Qtree拷贝构造函数中调用copy:
root=QtreeNode::copy(*tree.root); //each tree has a root pointer
//have also tried many different things here, but dont really know what to put
我收到以下错误:
error: cannot call member function ‘Qtree::QtreeNode* Qtree::QtreeNode::copy(Qtree::QtreeNode&)’ without object
and
error: no matching function for call to ‘copy(Qtree::QtreeNode&)’
答案 0 :(得分:2)
尝试:
static QuadtreeNode copy(QuadtreeNode & n);
或更好地创建复制构造函数。
copy
是实例方法,它意味着它只能在提供对象的情况下运行。它的内部签名是一个函数:
QuadtreeNode copy(QtreeNode* this, QuadtreeNode & n);
所以你必须传递给参数。使用了表示法obj->copy(..)
,但它实际上是将obj
作为第一个参数传递。
答案 1 :(得分:1)
您需要一个类Qtree :: QtreeNode的实例才能在其上调用copy()方法。
你做不到:
root = Qtree::QtreeNode::copy(*tree.root);
但你可以这样做:
Qtree::QtreeNode myQtree;
root = myQtree.copy(*tree.root);
答案 2 :(得分:0)
QtreeNode Qtree::QtreeNode::copy(QtreeNode & n) {
QtreeNode *newNode;
//....
return newNode;
}
我可能错了,但是您将函数定义为返回(QtreeNode),然后不返回(QtreeNode *)?