1

Im new to C++. I get error on the statements marked //this shows error but its alternative (marked as but this works ). Can someone explain this please?

#include <bits/stdc++.h>
using namespace std;

class Node
{
public:
    Node *next;
    int data;
};

class Que
{
public:
    //this shows error
    /* 
    Node *front, *rear;
    front = NULL;
    rear = NULL;
    */

    //but this works
    Node *front = NULL, *rear = NULL;
};
  • 2
    [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h), [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Jul 26 '20 at 08:19
  • okay next time i will correct it. Thanks! @Evg – Cams Boyfriend Jul 26 '20 at 08:27
  • It's the difference between a declaration of a member with initialisation (`Node *front = NULL`) and assignment (`front = NULL`, which relies on `front` having being previously declared). An assignment is only permitted within a function. Initialisation is permitted when the name is declared/defined, and a declaration is possible in both a class definition and within a function (among others). – Peter Jul 26 '20 at 08:28
  • @Peter Thank you for your reply. So you mean its better to declare with initialisation or else use function/constructor. Right? – Cams Boyfriend Jul 26 '20 at 08:31
  • @CamsBoyfriend - That's the way it works out if, by "better", you mean "valid in rules of the language". What you were attempting is not valid. – Peter Jul 26 '20 at 09:09

3 Answers3

3

These:

front = NULL;
rear = NULL;

are assignment statements. And this:

Node *front = NULL, *rear = NULL;

is a declaration (definition, initialization) statement using in-class initializers.

Assignment statements are not allowed to appear in the class declaration body, whereas the initialization statements are.

Ron
  • 14,674
  • 4
  • 34
  • 47
0

You need a function or a class constructor to initialize the class objects:

class Node
{
public:
    Node *next;
    int data;
};

class Que
{
public:
    Que() {
        Node *front, *rear;
        front = NULL;
        rear = NULL;
    }
};

In the aforementioned code, the front and rear have now a storage class since they're in constructor which is acting like a function.


To clarify better, you may use the following code:

class test {
public:
    int x;    // declaring a variable outside of a constructor or function
    x = 10;   // initialization is not allowed here
}
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
0
#define NULL nullptr
class Node
{
public:
    Node* next;
    int data;
};

class Que1 {
public: //Method 1 - initializer list
    Node* front{ NULL }, * rear{ NULL };
};
class Que2 {
public: //Method 2 - assignment operator for initialization
    Node* front = NULL, * rear = NULL;
};
class Que3 {
public: //Method 3 - initializer list at constructor
    Node* front, * rear;
    Que3() : front{ NULL }, rear{ NULL }
    {}
};
class Que4 {
public: //Method 4 - assignment after member intialization/construction
    Node* front, * rear;
    Que4()
    {
        front = NULL;
        rear = NULL;
    }
};

Your assignments were not on a function's body. since you tried to initialize, I thought about showing different syntax. method 1 is the most preferred one.

Dharman
  • 30,962
  • 25
  • 85
  • 135