Bloodshed Dev-C++ compiler errors *Binary Trees

拈花ヽ惹草 提交于 2019-12-25 16:57:47

问题


I would like feedback on my code please. It is an assignment for school where we were asked to write a function that swaps the left and right side binary trees. Our class that our professor gave us was swapBinaryTrees and the rest was left up to us. I am getting a whole lot of compiler errors and I am not sure where I am going wrong with my syntax. I am getting errors when I compile it like line 14 expected init-declarator '<'token the line in reference is

void binaryTreeSearch<elemType>::swapSubtreeNodes()

I have the same error for line 19 which is

void binaryTreeSearch<elemType>::swapSubtreeNodes(nodeType<elemType> *p)

for both of the above I have another error that says expecting ';' before '<' token Then I get undeclared identifier in my main function for

binaryTreeSearch<int> tree;

I also am getting expected primary expression before "int" and expected ';' before "int" then it tells me that cout and cin are not declared I do not know what the haybales is going on here. I will post my whole code below, any help is greatly appreciated.

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;


}

回答1:


You didn't declare the binaryTreeSearch type. Therefore you cannot implement member functions for it. Your compiler is telling you that it has no clue what you mean by

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

at the position with < because it doesn't understand that you intend binaryTreeSearch to be a class.


What you are doing when you write:

template <class elemType>
void binaryTreeSearch<elemType>::swapSubtreeNodes()
{
     swapSubtreeNodes(root);
}

is this; You are telling C++ that you intend to implement a member function swapSubTreeNodes of type int ()() that belongs to a structured type (i.e. a class or a struct) of the name binaryTreeSearch which is templated with one parameter. However, your compiler (any compiler really) complains because there is no such type. How you fix this issue depends on what you really intended to do. One option could be declaring that type:

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>*);
};

This will not fix all errors but is probably what you intended to do. You may also want to add a proper constructor and destructor.



来源:https://stackoverflow.com/questions/12438661/bloodshed-dev-c-compiler-errors-binary-trees

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!