class 模板 "Node" 的参数列表是 missingC/C++(441)
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 "
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);
}
}
我是C++的初学者,我正在尝试学习基本的二叉树打印,我不知道如何修复void print(const Node *tree)
中Node
下的红线,我看到有人说将模板 T 转换为 int 会修复错误,但我不知道为什么会这样。
打印函数也应该是模板函数,C++编译器需要你告诉它打印函数中Node的具体类型。见下文:
#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);
}
#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 "
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);
}
}
我是C++的初学者,我正在尝试学习基本的二叉树打印,我不知道如何修复void print(const Node *tree)
中Node
下的红线,我看到有人说将模板 T 转换为 int 会修复错误,但我不知道为什么会这样。
打印函数也应该是模板函数,C++编译器需要你告诉它打印函数中Node的具体类型。见下文:
#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);
}