开发者

Problem with my method in C++

I have a class like so:

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);

   };

And so the question is on the copy method. It makes a copy of a given node and returns it.

QtreeNod开发者_如何学Ce Qtree::QtreeNode::copy(QtreeNode & n) {
   QtreeNode *newNode;
   //....
   return newNode;
}

And then I call copy from my Qtree copy constructor:

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

I get the following errors:

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&)’


try:

static QuadtreeNode copy(QuadtreeNode & n);

or better create copy constructor.

copy is instance method, it means that it can be run only with object provided. It's internal signature as a function is:

QuadtreeNode copy(QtreeNode* this, QuadtreeNode & n);

So you have to pass to parameters. Notation obj->copy(..) is used, but it is actually passing obj as first parameter.


You need an instance of your class Qtree::QtreeNode to be able to call the copy() method on it.

You cannot do :

root = Qtree::QtreeNode::copy(*tree.root);

But you can do :

Qtree::QtreeNode myQtree;
root = myQtree.copy(*tree.root);


QtreeNode Qtree::QtreeNode::copy(QtreeNode & n) {
   QtreeNode *newNode;
   //....
   return newNode;
}

I could be wrong, but you define the function as returning a (QtreeNode), then don't you return a (QtreeNode *) ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜