-4

I can use this in main,insert and Display By Level as counter because i need the hight or the Tree

class BinarySearchTree
{
public:
        Node* root;
        int countHight=0; //in this line
        BinarySearchTree()
        { root = NULL; }
        ~BinarySearchTree() 
        { return; }
        void insert(int value);
        void display(Node* temp);
        void DisplayByLevel(Node* temp,int level); 
};
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • 1
    Whar's actually unclear about the warning message? If you have an older standard compiler simply use the constructor initializer list to initialize the member variable. – πάντα ῥεῖ Apr 14 '19 at 17:14
  • 1
    Well the error says it clearly. Either compile with c++11 or higher or move the initialization of `countHight` into the constructor – derpirscher Apr 14 '19 at 17:15
  • You could just add the `-std=c++11` compiler option as the warning message is suggesting. If you don't want to do that, set countHight=0 in the constructor instead. – Elias Apr 14 '19 at 17:15
  • Learn how to [format code in posts properly](https://stackoverflow.com/editing-help) please. – πάντα ῥεῖ Apr 14 '19 at 17:21

1 Answers1

0

A C++ class definition is like a blueprint for something that doesn't exist yet, as such until you actually create an instance of the class, there is no variable to set to zero at initialisation. This is what the compiler is complaining about.

The only time that this would be valid is if the variable was declared static, but this would mean that every single instance of the class would effect the single static variable.

There are two solutions to this, as stated in the comments you could simply tell the compiler to use the C++11 standards which allow this method of initialisation, or you can use the more common and compatible method with older compilers which is to initialise it in the constructor (as you are already doing for root), like so:

class BinarySearchTree
{
public:
        Node* root;
        int countHight;

        BinarySearchTree()
        {
           root = NULL;
           countHight = 0;
        }

        ~BinarySearchTree() 
        {
          return;
        }

        void insert(int value);
        void display(Node* temp);
        void DisplayByLevel(Node* temp,int level); 
};
halfer
  • 19,824
  • 17
  • 99
  • 186
Geoffrey
  • 10,843
  • 3
  • 33
  • 46