【问题标题】:error when using this in c++在 C++ 中使用 this 时出错
【发布时间】:2013-01-19 07:03:32
【问题描述】:
class Tower
{
int index;//index of the tower;
nodeStack<int> t;
int size;//number of disks in the tower;

public:
Tower(int in);
void moveTopTo(Tower&);
void move(int size,Tower& dest,Tower& buffer);

};

void Tower::move(int n,Tower& dest,Tower& buffer)
{
    if(n>0)
    {
    move(n-1,buffer,dest);
    moveTopTo(dest);
    move(n-1,dest,this);
    }
}

在这一行:move(n-1,dest,this);

得到一个错误,说非常量引用的初始值必须是左值;

【问题讨论】:

    标签: c++ lvalue towers-of-hanoi gwt-history


    【解决方案1】:

    您需要取消引用您的 this 指针,以便您可以将其用作参考:

    move(n-1, dest, *this);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多