用于语法错误的术语横向模板,寻找&#x27 ;;;
我创建了一个用于二进制树的横向横向的模板。在声明堆栈时,我会遇到语法错误。它期望一个';'但是我看不到我会放在哪里';
我可以将其运行以使用int类型而不是t类型运行。我想知道我在模板中做错了什么。
#include "Node.h"
#include <stack>
#include <iostream>
template <typename T>
void inOrderTraversal(Node<T>* root)
{
stack<Node<T>*> s; // BUILD ERROR "UNEXPECTED IDENTTIFIER, EXPECTTED ';'
Node<T>* curr = root;
// Initially s.empty==true
while (curr != NULL || s.empty() == false)
{
/* Reach the left most Node of the
curr Node */
while (curr != NULL)
{
/* place pointer to a tree node on
the stack before traversing
the node's left subtree */
s.push(curr);
curr = curr->left;
// end of inner whitl
}
/* Current must be NULL at this point */
curr = s.top();
s.pop();
// print
cout << curr->data << " ";
/* we have visited the node and its
left subtree. Now, it's right
subtree's turn */
curr = curr->right;
} /* end of while */
}
// function to create binary tree node on the heap
template <typename T>
Node<T>* newNodeOnHeap(T data) {
Node* nodetoreturn = new Node; // make node on the heap
nodetoreturn->value = data;
nodetoreturn->left = nullptr;
nodetoreturn->right = nullptr;
return &nodetoreturn;
}
// function to delete binary tree node on the heap
template <typename T>
void deleteNodeOnHeap(T& node){
delete node; // delete node
}
int main() {
// make a binary tree on the heap
int data = 10;
Node<int>* root = newNodeOnHeap(data);
root->left = newNodeOnHeap(5);
root->right = newNodeOnHeap(20);
root->left->left = newNodeOnHeap(3);
root->left->right = newNodeOnHeap(7);
root->right->right = newNodeOnHeap(30);
// In order transversal of binary tree
inOrderTraversal(root);
}
这是错误 stack&lt; node&lt; t&gt;*&gt; s //构建错误“意外标识符,预期';'
这是node.h文件
#define NODE_H_JAMES
template <typename T>
class Node
{
public:
T value;
Node* left;
Node* right;
// constructor
Node(T val)
{
this->value = val;
left = right = nullptr;
}
};
I created a template for an inorder transverse of a binary tree. I get a syntax error in declaring the stack. It expects a ';' but I do not see where I would put an ';'
I can get it to run with int type instead of T type. I wonder what I am doing wrong in the template.
#include "Node.h"
#include <stack>
#include <iostream>
template <typename T>
void inOrderTraversal(Node<T>* root)
{
stack<Node<T>*> s; // BUILD ERROR "UNEXPECTED IDENTTIFIER, EXPECTTED ';'
Node<T>* curr = root;
// Initially s.empty==true
while (curr != NULL || s.empty() == false)
{
/* Reach the left most Node of the
curr Node */
while (curr != NULL)
{
/* place pointer to a tree node on
the stack before traversing
the node's left subtree */
s.push(curr);
curr = curr->left;
// end of inner whitl
}
/* Current must be NULL at this point */
curr = s.top();
s.pop();
// print
cout << curr->data << " ";
/* we have visited the node and its
left subtree. Now, it's right
subtree's turn */
curr = curr->right;
} /* end of while */
}
// function to create binary tree node on the heap
template <typename T>
Node<T>* newNodeOnHeap(T data) {
Node* nodetoreturn = new Node; // make node on the heap
nodetoreturn->value = data;
nodetoreturn->left = nullptr;
nodetoreturn->right = nullptr;
return &nodetoreturn;
}
// function to delete binary tree node on the heap
template <typename T>
void deleteNodeOnHeap(T& node){
delete node; // delete node
}
int main() {
// make a binary tree on the heap
int data = 10;
Node<int>* root = newNodeOnHeap(data);
root->left = newNodeOnHeap(5);
root->right = newNodeOnHeap(20);
root->left->left = newNodeOnHeap(3);
root->left->right = newNodeOnHeap(7);
root->right->right = newNodeOnHeap(30);
// In order transversal of binary tree
inOrderTraversal(root);
}
This is the errorstack<Node<T>*> s; // BUILD ERROR "UNEXPECTED IDENTIFIER, EXPECTED ';'
This is the Node.h file
#define NODE_H_JAMES
template <typename T>
class Node
{
public:
T value;
Node* left;
Node* right;
// constructor
Node(T val)
{
this->value = val;
left = right = nullptr;
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论