c++ - Bloodshed Dev-C++ 编译器错误 *二叉树

标签 c++ compiler-errors binary-tree dev-c++

我想请反馈我的代码。这是学校的作业,我们被要求编写一个交换左右二叉树的函数。我们教授给我们的课是 swapBinaryTrees,剩下的就交给我们了。我收到了很多编译器错误,我不确定我的语法哪里出了问题。当我像第 14 行预期的 init-declarator '<'token 引用中的行一样编译它时出现错误

void binaryTreeSearch<elemType>::swapSubtreeNodes()

我对第 19 行有相同的错误,即
void binaryTreeSearch<elemType>::swapSubtreeNodes(nodeType<elemType> *p)

对于上述两种情况,我还有另一个错误提示“;”在 '<' 标记之前
然后我在我的主函数中得到了未声明的标识符
binaryTreeSearch<int> tree;

我也在“int”和预期的';'之前得到预期的主要表达在“int”之前
然后它告诉我没有声明 cout 和 cin 我不知道这里发生了什么。我将在下面发布我的整个代码,非常感谢任何帮助。
template <class elemType>
struct nodeType
{
       elemType info;
       nodeType<elemType> *lLink;
       nodeType<elemType> *rLink;
};
template <class elemType>
class swapSubtreeNodes
{

};
template <class elemType>
void binaryTreeSearch<elemType>::swapSubtreeNodes()
{
     swapSubtreeNodes(root);
}
template <class elemType>
void binaryTreeSearch<elemType>::swapSubtreeNodes(nodeType<elemType> *p)
{
    root = temp;
    nodeType<elemType> *root;
    nodeType<elemType> *temp;
    if (p == null)
    {
          return;
    }
    else
    {
        swapSubtreeNodes(p->lLink);
        swapSubtreeNodes(p->rLink);                                  
        temp = p->lLink;
        p->lLink = p->rLink;
        p->rLink = temp;
    }
}
int main()
{
    binaryTreeSearch<int> tree;
    int num;
    cout << "This is how we swap'em" << endl;
    cout << "Insert number (press enter after each one entered)." << endl;
    cout << "Enter -999 to complete" << endl;
    tree.insert(0);
    cin >> num;
    while (num != -999)
    {
          tree.insert(num);
          cin >> num;
    }
    cout << "Your swapped binary tree is " << endl;
         tree.swapSubtreeNodes();
         tree.printTree();
         cout << endl;


}

最佳答案

您没有声明 binaryTreeSearch类型。因此你不能为它实现成员函数。你的编译器告诉你它不知道你的意思

template <class elemType>
void binaryTreeSearch<elemType>::swapSubtreeNodes()

< 的位置因为它不明白你的意图 binaryTreeSearch成为一个类(class)。

你写的时候在做什么:
template <class elemType>
void binaryTreeSearch<elemType>::swapSubtreeNodes()
{
     swapSubtreeNodes(root);
}

这是;你告诉 C++ 你打算实现一个成员函数 swapSubTreeNodes类型 int ()()属于名称 class 的结构化类型(即 structbinaryTreeSearch )这是用一个参数模板化的。但是,您的编译器(实际上是任何编译器)都会提示,因为没有这样的类型。您如何解决此问题取决于您真正打算做什么。一种选择是声明该类型:
template <class elemType>
class binaryTreeSearch // I really recommend to write types Uppercase!
{
  private:
    nodeType<elemType>* root;
    //                  ^---- that's the variably you are trying to
    //                        access in your original post
  public:
    void swapSubtreeNodes();
    void swapSubtreeNodes(nodeType<elemType>*);
};

这不会修复所有错误,但可能是您打算做的。您可能还想添加适当的构造函数和析构函数。

关于c++ - Bloodshed Dev-C++ 编译器错误 *二叉树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12438661/

相关文章:

c++ - C++ 库中的快速梯度下降实现?

c++ - 'class' 类型重定义,已经包含 ifndef/define/endif

java - “找不到符号”或“无法解析符号”错误是什么意思?

c++ - 仅使用小于运算符查找两个变量是否相等

Lisp - 后序二叉树的问题

c# - 通过代码获取文件 tnsnames.ora 的位置

Scala 隐式参数作用域

c++ - 如何在Windows上使用MinGW编译C++?

Python 按后序对节点进行编号

c++ - "hard-coding"和传入有关内存的参数有什么区别?