简体   繁体   English

C ++中的非静态成员

[英]Non static members in C++

  1. error C2648: 'stack::Y' : use of member as default parameter requires static member 错误C2648:“堆栈:: Y”:使用成员作为默认参数需要静态成员
  2. error C2648: 'stack::X' : use of member as default parameter requires static member 错误C2648:“堆栈:: X”:使用成员作为默认参数需要静态成员
  3. IntelliSense: a nonstatic member reference must be relative to a specific object IntelliSense:非静态成员引用必须相对于特定对象
  4. IntelliSense: a nonstatic member reference must be relative to a specific object IntelliSense:非静态成员引用必须相对于特定对象

Please, help to fix it 请帮忙解决

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

(this is a correction to my old answer, which was incorrect) (这是对我的旧答案的更正,这是不正确的)

It seems that you want to use a non-static member as a default value for a parameter, and the compiler tells you this is impossible. 似乎您想使用非静态成员作为参数的默认值,并且编译器告诉您这是不可能的。 You can use an overload as a workaround: 您可以使用重载作为解决方法:

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

Some people would say overloading is better than using default values, because you probably don't want to support calling makelist with 1 parameter, only with 0 or 2 parameters (or, if you actually want this, you can add another overload, with 1 parameter). 有人会说重载比使用默认值更好,因为您可能不希望仅使用0或2个参数来支持仅使用1个参数调用makelist (或者,如果您确实希望这样做,则可以使用1来添加另一个重载。参数)。

You have a spurious "=" character in your code; 您的代码中有一个伪造的“ =”字符; replace your line with error with the following: 用以下错误替换您的行:

void makelist(int X, int Y);

The "=" character makes it look like the declaration has default parameters whose values are X and Y , which is totally not what you intended to do. “ =”字符使它看起来像声明具有默认参数,其值为XY ,这完全不是您要执行的操作。

In addition, it is customary to have the same parameter names in declaration and definition: 另外,习惯上在声明和定义中使用相同的参数名称:

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
{
    ...
}

Get rid of those = signs in the function declaration: 消除函数声明中的那些=符号:

void makelist(int x, int y);

so it's just like the definition: 所以就像定义:

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

Presuming you meant to use default values 假设您打算使用默认值

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

This is not allowed as the default values must be compiletime constants or compiletime addressable, which members of a not instantiated class are not. 不允许这样做,因为默认值必须是编译时常量或可编译时可寻址的,而未实例化类的成员则不允许。 The compiler needs an address be able to generate the code. 编译器需要一个能够生成代码的地址。

You can overload the function 您可以重载该功能

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

And so get nearly the same behaviour as you asked. 因此,您得到的行为几乎与您要求的相同。

If you have a problem with _X & _Y then its because the compiler reserves _??? 如果您对_X和_Y有问题,则是因为编译器保留了_ ??? for itself or libraries. 本身或图书馆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM