C++ main.cpp:65:16:错误:在';之前应为主表达式*';代币

C++ main.cpp:65:16:错误:在';之前应为主表达式*';代币,c++,C++,大家好,我已经编写了一个与代码相关的二进制搜索树,如果你们看一下我在“findnode”函数中的代码行,它是 node<tree<int>*> obj; 所有这些错误都与我上面提到的行有关。那么我该如何解决这个问题呢 我的代码: #include<iostream> #define null NULL using namespace std; template<typename T> class tree { private: T va

大家好,我已经编写了一个与代码相关的二进制搜索树,如果你们看一下我在“findnode”函数中的代码行,它是

node<tree<int>*> obj;
所有这些错误都与我上面提到的行有关。那么我该如何解决这个问题呢

我的代码:

#include<iostream>
#define null NULL
using namespace std;

template<typename T>
class tree {
private:
    T val;
    tree<T>*  left;
    tree<T>* right;

public:
    tree()
    {
        left = null;
        right = null;
    }
    tree<T>* addnode(tree<int>* root, T info);
    void printinorder(tree<int>*);
    void findnode(tree<int>* root);
    void findlevel(tree<int>* enter, int num);

};
template<typename T>
tree<T>* tree<T>::addnode(tree<int>* root, T info)
{
    if (root == null)
    {
        tree* root = new tree;
        root->val = info;
        return root;
    }
    else if (root->val > info)
    {
        root->left = addnode(root->left, info);
    }
    else if (root->val < info)
    {
        root->right = addnode(root->right, info);
    }
    else
        cout << info << " is already exists in tree." << endl;
    return root;
}
template<typename T>
void tree<T>::printinorder(tree<int>* root)
{
    if (root != null)
    {
        if (root->left != null)
        {
            printinorder(root->left);
        }
        cout << root->val << " ";
        if (root->right != null)
        {
            printinorder(root->right);
        }

    }
}
template<typename T>
void tree<T>::findnode(tree<int>* root)
{
    node<tree<int>*> obj;
    tree<int>* help;
    help = root;
    obj.push(help);
    while (!obj.isEmpty())
    {
        help = obj.pop();
        findlevel(root, help->val);
        if (help->left != null)
        {
            obj.push(help->left);
        }
        if (help->right != null)
        {
            obj.push(help->right);
        }
    }
}
template<typename T>
void tree<T>::findlevel(tree<int>* enter, int num)
{
    static int count;
    if (enter == null)
    {
        cout << enter->val << " is not in your tree" << endl;
    }
    else if (enter->val == num)
    {
        cout << num << "----->>>>level :" << count << endl;
        count = 0;
    }
    else
    {
        count++;
        if (enter->val > num)
        {
            findlevel(enter->left, num);
        }
        else
        {
            findlevel(enter->right, num);
        }
    }
}

template<typename T>
class node {
private:
    T data;
    node* next;
    node* top;
    node* head;
    node* back;
public:
    node()
    {
        head = null;
        top = null;
        next = null;
        back = null;
    }
    void push(T info1)
    {
        if (head == null)
        {
            node* n = new node;
            n->data = info1;
            head = n;
            top = n;
        }
        else
        {
            node* n = new node;
            n->data = info1;
            n->next = top;
            top = n;
        }

    }
    T pop()
    {
        T d;
        d = head->data;
        if (top != head)
        {
            back = top;
            while (back->next != head)
            {
                back = back->next;
            }
            head = back;
            return d;
        }
        else
        {
            head = null;
            return d;
        }

        return d;

    }
    bool isEmpty()
    {
        return head == null;
    }
};
int main()
{
    tree<int>* shoh = null;
    char ch;
    int i;
    while (true)
    {
        cin >> ch;
        switch (ch)
        {
        case 'i': cin >> i;
            shoh = shoh->addnode(shoh, i);
            break;
        case 'p': shoh->printinorder(shoh);
            break;
        case 't':
            shoh->findnode(shoh);
            break;
        case 'e': exit(0);
            break;
        }
    }
    return 0;
}
#包括
#定义空值
使用名称空间std;
样板
类树{
私人:
T值;
树*左;
树*对;
公众:
树()
{
左=空;
右=空;
}
树*添加节点(树*根,T信息);
无效打印顺序(树*);
void findnode(树*根);
void findlevel(树*enter,int num);
};
样板
树*树::addnode(树*根,T信息)
{
if(root==null)
{
树*根=新树;
root->val=info;
返回根;
}
否则如果(根->值>信息)
{
根->左=添加节点(根->左,信息);
}
else if(root->val右=添加节点(根->右,信息);
}
其他的
不正确);
}
}
}
样板
void tree::findnode(树*根)
{
节点obj;
树*帮助;
帮助=根;
对象推送(帮助);
而(!obj.isEmpty())
{
help=obj.pop();
findlevel(root,help->val);
如果(帮助->左!=null)
{
对象推送(帮助->左);
}
如果(帮助->正确!=null)
{
对象推送(帮助->右侧);
}
}
}
样板
void tree::findlevel(tree*enter,int num)
{
静态整数计数;
如果(输入==null)
{
cout val left,num);
}
其他的
{
findlevel(输入->右侧,数值);
}
}
}
样板
类节点{
私人:
T数据;
节点*下一步;
节点*顶部;
节点*头;
节点*返回;
公众:
节点()
{
head=null;
top=null;
next=null;
back=null;
}
无效推送(T info1)
{
if(head==null)
{
node*n=新节点;
n->data=info1;
水头=n;
top=n;
}
其他的
{
node*n=新节点;
n->data=info1;
n->next=顶部;
top=n;
}
}
T pop()
{
td;
d=头部->数据;
如果(顶部!=头部)
{
背面=顶部;
while(后退->下一步!=头部)
{
后退=后退->下一步;
}
头=背;
返回d;
}
其他的
{
head=null;
返回d;
}
返回d;
}
布尔是空的
{
返回头==null;
}
};
int main()
{
tree*shoh=null;
char ch;
int i;
while(true)
{
cin>>ch;
开关(ch)
{
案例“i”:cin>>i;
shoh=shoh->addnode(shoh,i);
打破
案例“p”:shoh->printinorder(shoh);
打破
案例“t”:
shoh->findnode(shoh);
打破
案例“e”:退出(0);
打破
}
}
返回0;
}

您正在文件的更下方定义类型
节点。当你在第65行引用它时,它还不知道。在C++中定义“类代码>树> <代码>类。

在C++中,您应该使用<代码> nulLPTR < /代码>代替<代码> null <代码>。当然不需要
#定义null null
,我也不建议
#定义null nullptr
。正如错误所解释的那样,
节点在使用它的地方没有声明,只是在以后定义。在使用之前,您必须声明您所使用的任何东西。@FrançoisAndrieux为什么不
#定义null nullptr
?这应该是找到bug的一个好方法:)事实上,这是为了快速输入,而不是总是用大写字母或nullptr写NULL。但无论如何,谢谢。我很感激哦,谢谢你。我不知道我为什么不注意这种简单的错误。这是我的错。也许考试后我太累了
#include<iostream>
#define null NULL
using namespace std;

template<typename T>
class tree {
private:
    T val;
    tree<T>*  left;
    tree<T>* right;

public:
    tree()
    {
        left = null;
        right = null;
    }
    tree<T>* addnode(tree<int>* root, T info);
    void printinorder(tree<int>*);
    void findnode(tree<int>* root);
    void findlevel(tree<int>* enter, int num);

};
template<typename T>
tree<T>* tree<T>::addnode(tree<int>* root, T info)
{
    if (root == null)
    {
        tree* root = new tree;
        root->val = info;
        return root;
    }
    else if (root->val > info)
    {
        root->left = addnode(root->left, info);
    }
    else if (root->val < info)
    {
        root->right = addnode(root->right, info);
    }
    else
        cout << info << " is already exists in tree." << endl;
    return root;
}
template<typename T>
void tree<T>::printinorder(tree<int>* root)
{
    if (root != null)
    {
        if (root->left != null)
        {
            printinorder(root->left);
        }
        cout << root->val << " ";
        if (root->right != null)
        {
            printinorder(root->right);
        }

    }
}
template<typename T>
void tree<T>::findnode(tree<int>* root)
{
    node<tree<int>*> obj;
    tree<int>* help;
    help = root;
    obj.push(help);
    while (!obj.isEmpty())
    {
        help = obj.pop();
        findlevel(root, help->val);
        if (help->left != null)
        {
            obj.push(help->left);
        }
        if (help->right != null)
        {
            obj.push(help->right);
        }
    }
}
template<typename T>
void tree<T>::findlevel(tree<int>* enter, int num)
{
    static int count;
    if (enter == null)
    {
        cout << enter->val << " is not in your tree" << endl;
    }
    else if (enter->val == num)
    {
        cout << num << "----->>>>level :" << count << endl;
        count = 0;
    }
    else
    {
        count++;
        if (enter->val > num)
        {
            findlevel(enter->left, num);
        }
        else
        {
            findlevel(enter->right, num);
        }
    }
}

template<typename T>
class node {
private:
    T data;
    node* next;
    node* top;
    node* head;
    node* back;
public:
    node()
    {
        head = null;
        top = null;
        next = null;
        back = null;
    }
    void push(T info1)
    {
        if (head == null)
        {
            node* n = new node;
            n->data = info1;
            head = n;
            top = n;
        }
        else
        {
            node* n = new node;
            n->data = info1;
            n->next = top;
            top = n;
        }

    }
    T pop()
    {
        T d;
        d = head->data;
        if (top != head)
        {
            back = top;
            while (back->next != head)
            {
                back = back->next;
            }
            head = back;
            return d;
        }
        else
        {
            head = null;
            return d;
        }

        return d;

    }
    bool isEmpty()
    {
        return head == null;
    }
};
int main()
{
    tree<int>* shoh = null;
    char ch;
    int i;
    while (true)
    {
        cin >> ch;
        switch (ch)
        {
        case 'i': cin >> i;
            shoh = shoh->addnode(shoh, i);
            break;
        case 'p': shoh->printinorder(shoh);
            break;
        case 't':
            shoh->findnode(shoh);
            break;
        case 'e': exit(0);
            break;
        }
    }
    return 0;
}