简体   繁体   中英

Non static members in C++

  1. error C2648: 'stack::Y' : use of member as default parameter requires static member
  2. error C2648: 'stack::X' : use of member as default parameter requires static member
  3. IntelliSense: a nonstatic member reference must be relative to a specific object
  4. IntelliSense: a nonstatic member reference must be relative to a specific object

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

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.

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 _??? for itself or libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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