Home > Back-end >  argument list for class template "Node" is missingC/C (441)
argument list for class template "Node" is missingC/C (441)

Time:04-10

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

CodePudding user response:

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);
}
  • Related