无法从类模板输出字符串

Can not output a string from a class template

本文关键字:输出 字符串      更新时间:2023-10-16

我一直在尝试让我的类模板为我打印出一个字符串。但是我得到了一个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");
}

我更改了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");
}