Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Default parameter in a function wouldnt compile/link without static inline

i was writing a BST class in C++ and wanted to add a default parameter to a function but, when i tried to the visual studio compiler gave compile and linking errors, so after a bit of googling, i made the root variable static inline and it worked, i want to know what does static inline change in this specific situation.

here’s the BST class code:

#pragma once

template<typename T>
class BST
{
   template<typename T>
   struct node
   {
    T data;
    node<T>* left;
    node<T>* right;

    node(T d)
    {
        data = d;
        left = right = nullptr;
    }
  };


public:

BST()
{
    root = nullptr;
}

~BST()
{
    delete root;
}

node<T>* insert(T d)
{
    if (root == nullptr)
    {
        root = new node<T>(d);

        return root;
    }
    
    node<T>* current = root;
    node<T>* k = nullptr;
    

    while (current != nullptr)
    {
        k = current;

        if (current->data > d)
        {
            current = current->left;
        }
        else if (current->data < d)
        {
            current = current->right;
        }
    }

    if (k->data > d)
    {
        k->left = new node<T>(d);
    }
    else if (k->data < d)
    {
        k->right = new node<T>(d);
    }

    
    return root;
}


bool find(T d)
{
    if (root == nullptr)
    {
        std::cerr << "Tree is empty! : ";

        return false;
    }

    node<T>* current = root;
    node<T>* k = nullptr;

    while (current->data != d)
    {
        k = current;

        if (current->left == nullptr || current->right == nullptr && current->data != d)
        {
            std::cout << "Not Found! : ";
            
            return false;
        }

        if (current->data > d)
        {
            current = current->left;
        }
        else if (current->data < d)
        {
            current = current->right;
        }
    }

    return true;
}

node<T>* findmax()
{
    node<T>* temp = root;
    while (temp->right != nullptr)
    {
        temp = temp->right;
    }

    return temp;
}

node<T>* findmin(node<T>* temp = root)
{
    
    while (temp->left != nullptr)
    {
        temp = temp->left;
    }

    return temp;
}

private:
static inline node<T>* root;

};

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is because

The keyword this shall not be used in a default argument of a member function

and non static member of a class is bound to this (i.e. root is equivalent to this->root). More about default parameters restrictions here.

In your case you could have the following workaround:

node<T>* findmin(node<T>* temp = nullptr)
{
    if (temp == nullptr) temp = root;

however I am not sure if you should use it, for the sake of clarity.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading