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

argument list for class template "Node" is missingC/C++(441)

#include <iostream>
using namespace std;
template <typename T>
// we shouln't use the template here, it will cause a bug
// "argument list for class template "Node" is missingC/C++(441)"
// to fix the bug, check "https://stackoverflow.com/questions/15283195/argument-list-for-class-template-is-missing"
struct Node
{
    T datum;
    Node* left;
    Node* right;
};

// REQUIRES: node represents a valid tree
// MODIFIES: cout 
// EFFECTS: Prints each element in the given tree to stanhdard out
//          with each element followed by a space
void print(const Node *tree)  {
    if(tree) {            // non-empty tree
        cout << tree->datum << " ";
        print(tree->left);
        print(tree->right);
    }
}

I am a beginner in C++, I was trying to learn basic Binary tree print, I don’t know how to fix the red line under Node in void print(const Node *tree), I saw some people saying that convert the template T to int would fix the bug, but I don’t know why that works.

>Solution :

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

The print function should also be a template function, the C++ compiler needs you to tell it the specific type of the Node in print function. see below:

#include <iostream>
using namespace std;

template<typename T>
struct Node
{
    T datum;
    Node* left;
    Node* right;
};

template<typename T>
void print(const Node<T>* tree) {
    if (tree == nullptr) {
        return;
    }
    print(tree->left);
    cout << tree->datum << " ";
    print(tree->right);
}
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