【问题标题】:Non static members in C++C++ 中的非静态成员
【发布时间】:2014-11-04 06:50:29
【问题描述】:
  1. 错误 C2648: 'stack::Y' : 使用成员作为默认参数 需要静态成员
  2. error C2648: 'stack::X' : 使用成员作为默认参数 需要静态成员
  3. IntelliSense:非静态成员引用必须相对于 特定对象
  4. IntelliSense:非静态成员引用必须相对于 特定对象

请帮忙解决一下

class stack{
    node *head, *tail;
    int maze[10][10], X, Y, _X, _Y;
public:
    stack():head(0), tail(0){};
    ~stack();
    void load();
    void makelist(int = X, int = Y); //error is here
    void push(int, int);
    void pop();
    void print();
};
void stack::load(){
    ifstream fin("maze.txt");
    fin >> X >> Y >> _X >> _Y;
    cout << "Loaded matrix:" << endl << endl;
    for (int i = 0; i < 10; i++){
        for (int j = 0; j < 10; j++){
            fin >> maze[i][j];
            if (i == X && j == Y)
                cout << "S ";
            else if (i == _X && j == _Y)
                cout << "F ";
            else
                cout << maze[i][j] << " ";
        }
        cout << endl;
    }
}
void stack::makelist(int x, int y)
{
    if (x == _X && y == _Y)
    {
        push(x, y);
        print();
        pop();
        return;
    }
    if (x > 0) if (maze[x - 1][y] == 0) { maze[x][y] = 1; push(x, y); makelist(x - 1, y); pop(); maze[x][y] = 0; }
    if (x < 9) if (maze[x + 1][y] == 0) { maze[x][y] = 1; push(x, y); makelist(x + 1, y); pop(); maze[x][y] = 0; }
    if (y > 0) if (maze[x][y - 1] == 0) { maze[x][y] = 1; push(x, y); makelist(x, y - 1); pop(); maze[x][y] = 0; }
    if (y < 9) if (maze[x][y + 1] == 0) { maze[x][y] = 1; push(x, y); makelist(x, y + 1); pop(); maze[x][y] = 0; }
}

<...>

int main()
{
    stack obj;
    obj.load();
    obj.makelist();
    system("pause");
    return 0;
}

【问题讨论】:

  • 你想用void makelist(int = X, int = Y);做什么,默认参数?它说你不能做到这一点,而不是静态的。

标签: c++


【解决方案1】:

(这是对我旧答案的更正,这是不正确的)

您似乎想使用非静态成员作为参数的默认值,而编译器告诉您这是不可能的。您可以使用重载作为解决方法:

class stack{
    node *head, *tail;
    int maze[10][10], X, Y, _X, _Y;

public:
    void makelist() {makelist(X, Y);} // I added this overload to do what you want
    void makelist(int x, int x);
    ...
};

有些人会说重载比使用默认值更好,因为您可能不希望支持使用 1 个参数调用 makelist,仅使用 0 或 2 个参数(或者,如果您真的想要这个,您可以添加另一个重载,带有 1 个参数)。

【讨论】:

  • 谢谢!这就是我想要的
【解决方案2】:

您的代码中有一个虚假的“=”字符;用以下内容替换您的错误行:

void makelist(int X, int Y);

“=”字符使声明看起来具有默认参数,其值为XY,这完全不是您想要做的。

此外,习惯上在声明和定义中具有相同的参数名称:

void makelist(int x, int x); // declaration - I replaced X by x, Y by y
...
void stack::makelist(int x, int y) // definition - has lowercase names, which are good
{
    ...
}

【讨论】:

  • OP 将其称为makelist(),因此 OP 完全确实打算使用默认值。
  • 我正在尝试设置参数的默认值
  • 这个答案不正确;我会把它留在这里,只供 cmets 使用
【解决方案3】:

去掉函数声明中的=标志:

void makelist(int x, int y);

所以就像定义一样:

void stack::makelist(int x, int y)
{

【讨论】:

  • 假设他们没有尝试使用默认值...(注意push(int, int) 的签名)
【解决方案4】:

假设您打算使用默认值

void makelist(int x_ = X, int y_ = Y); //error is here

这是不允许的,因为默认值必须是编译时常量或编译时可寻址,而未实例化类的成员则不是。 编译器需要一个能够生成代码的地址。

你可以重载函数

void makelist(int x_, int y_);
void makelist() { makelist(X,Y); } 

因此得到几乎与您要求的行为相同的行为。

如果你对 _X & _Y 有问题,那是因为编译器保留了 _???为自己或图书馆。

【讨论】:

    猜你喜欢
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    相关资源
    最近更新 更多