0

我一直在尝试让我的课程模板为我打印出一个字符串。但是我得到了一个error C679: No operator found which takes a right hand operand of type std:: string. 我尝试了各种方法来使这个错误消失。

#include <iostream>
#include <string.h>


using namespace std;


template<class T>


class BinaryTree
{


  struct Node
  {
    T data;
    Node* lChildptr;
    Node* rChildptr;

    Node(T dataNew)
    {
      data = dataNew;
      lChildptr = NULL;
      rChildptr = NULL;
    }


  };
private:

  Node* root; 

  void Insert(Node* &newData, Node* &theRoot)
  {
    if(theRoot == NULL)
    {
      theRoot = new Node(newData);
      return;
    }

    else if(newData->data < theRoot->data)
      Insert(newData, theRoot->lChildptr);
    else
      Insert(newData, theRoot->rChildptr);;
  }   



  void insertnode(T item)
  {
    Node *newData;
    newData= new Node;
    newData->value = item;
    newData->lChildptr = newData = rChildptr = NULL;

    insert ( newData, root);
  }

  void PrintTree(Node* theRoot)
  {

    if(theRoot)
    {
      cout << theRoot->data << endl;
      PrintTree(theRoot->lChildptr); //<< The error is here 
      PrintTree(theRoot->rChildptr);
    }


  }

public:
  BinaryTree()
  {
    root = NULL;
  }

  void AddItem(T )
  {
    //Insert(newData, root);
  }

  void PrintTree()
  {
    PrintTree(root);
  }
};


int main()
{
  BinaryTree<string> *tree = new BinaryTree<string>();
  tree->AddItem("Erick");
  tree->PrintTree();

  system ( "pause");
}
4

1 回答 1

0

我更改void insertnode(T item)void Insert(Node* &newData, Node* &theRoot)取消注释 //Insert(newData, root);

这是固定代码...

#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;

template<class T>

class BinaryTree
{
    struct Node
    {
        T data;
        Node* lChildptr;
        Node* rChildptr;

        Node(T dataNew)
        {
            data = dataNew;
            lChildptr = NULL;
            rChildptr = NULL;
        }
    };
    private:

    Node* root; 

    void Insert(Node* newData, Node* &theRoot)
    {
        if(theRoot == NULL)
        {
            theRoot = newData;
            return;
        }
        else if(newData->data < theRoot->data)
            Insert(newData, theRoot->lChildptr);
        else
            Insert(newData, theRoot->rChildptr);;
    }   

    void insertnode(T item)
    {
        Node *newData;
        newData= new Node(item);

        Insert ( newData, root);
    }

    void PrintTree(Node* &theRoot)
    {
        if(theRoot)
        {
            cout << theRoot->data << endl;
            PrintTree(theRoot->lChildptr); //<< The error is here 
            PrintTree(theRoot->rChildptr);
        }
    }

    public:
    BinaryTree()
    {
        root = NULL;
    }

    void AddItem(T item)
    {
        insertnode(item);
    }

    void PrintTree()
    {
        PrintTree(root);
    }
};


int main()
{
    BinaryTree<string> *tree = new BinaryTree<string>();
    tree->AddItem("Erick");
    tree->PrintTree();

    system ( "pause");
}
于 2014-12-08T15:07:27.290 回答