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