没有名为BST的成员

时间:2016-11-04 11:31:14

标签: c++ operator-overloading binary-search-tree

我正在创建一个二叉树并尝试打印我传入的学生对象的名称。当我尝试打印树时,我收到一个错误:

  

tree.h:181:46:错误:'class samuel :: Student'没有名为'printInOrder'的成员

str += Node->get_data().printInOrder() + "\n";

这是我在主要使用

调用的功能

BSTree<Student>* student_tree = new BSTree<Student>; Student student = Student("Adam"); student_tree->insert(student); student_tree->printInOrder();

string printInOrder(){return inOrder(root, 0);}

private:
  string inOrder(BTNode<value_type>* Node, size_t level)
  {
    string str ="";
    if(Node != NULL)
    {
      str += inOrder(Node->get_right(), level++);
      for(int i = 1; i <= level; ++i)
      {
        str = str + "| ";
      }
      str += Node->get_data().printInOrder() + "\n";
      str += inOrder(Node->get_left(), level++);
    }
    return str;
  }

我不确定为什么当我尝试访问printInOrder时,它会通过Student。这是我的学生班

typedef Student value_type;
Student::Student()
{

}

Student::Student(std::string init_name, float init_grade)
{
    name = init_name;
    std::string studentName[50]={"Adam", "Cameron", "Jackson", "KiSoon", "Nicholas", "Adrian", "Chris", "Jacob", "Lance", "Ryan", 
    "Alexander", "Damian", "James", "Liam", "Sang", "Andrew", "David", "Jared", "Madison", "Shane", "Ashley", "Dillon", 
    "Jodi", "Magdalena", "Simon", "Benjamin", "Dylan", "Jonathan",  "Marcus", "Thomas", "Bradley", "Ethan" "Joshua", "Mark", 
    "Timothy", "Brobie", "Frederik", "Julius", "Melanie", "Trent", "Callan", "Hong", "Kelly", "Min", "Troy", "Callum", "Hugh", "Kenias", "Mitchell", "Zaanif"};
    for (int i = 0; i <50; i++)
    { 
        int j = (rand() % (i-1));
        string temp = studentName[j];
        studentName[j] = studentName[i];
        studentName[i] = temp;
    }   
}
Student::~Student()
{

}
void Student::set_name(string new_name)
{
    name = new_name;
}

const string Student::get_name() const
{
    return name;
}

void Student::set_grade(float new_grade)
{
    grade = new_grade;
}

float Student::get_grade()
{
    return grade;
}

我尝试的另一种方法是使用

string infix(BTNode<value_type>* Node)
{
  if (Node == NULL)
  {
    return "";
  }else{
    return (infix(Node->get_left()) + Node->get_data()) +
      infix(Node->get_right());
  }
}

friend ostream& operator << (ostream& out, const BSTree<value_type>& tree)
{
  out << tree.infix(tree.root) << endl;
  return out;
}

然后调用cout << student_tree << endl然而这打印了一个内存地址,任何人都能够澄清为什么会发生这种情况,谢谢

编辑:更改了我插入学生的方式。已将cout << student_tree << endl更改为cout << *student_tree << endl,错误

  

tree.h:70:9:错误:将'const samuel :: BSTree'传递为'this'参数会丢弃限定符[-fpermissive]

out << tree.infix(tree.root) << endl;

2 个答案:

答案 0 :(得分:1)

  

tree.h:181:46:错误:'class samuel :: Student'没有名为'printInOrder'的成员

Node->get_data()返回samuel::Student类型的对象,因为此编译器在printInOrder()类型中搜索samuel::Student。根据上面的代码,它不在那里。要解决此问题,请执行以下方法:

std::string Student::printInOrder()  
{
  // Return the data to be printed
}
  

student_tree-&gt; insert(* new Student());

看起来很可疑。树按值包含Student个对象。您在堆上创建一个Student对象,取消引用指针并将值复制到树中。之后指针丢失了。这将导致内存泄漏问题。

  

cout&lt;&lt; student_tree&lt;&lt;但是这会打印一个内存地址

因为它被声明为BSTree<Student>* student_tree。它是指向树的指针,因此输出正确,您打印地址。要打印树值,您需要取消引用指针:cout << *student_tree << endl

答案 1 :(得分:0)

继续其他答案......

  

然后调用cout&lt;&lt; student_tree&lt;&lt;然而这印刷了一个   内存地址,任何人都可以澄清为什么会发生这种情况   还有,谢谢

BSTree<Student>* student_tree = new BSTree<Student>;

student_tree是指向BSTree<Student>的指针,这意味着它保存BSTree对象的内存位置(内存地址),在这种情况下是一个未命名的对象。

您必须取消引用它才能通过*student_tree

获取地址中的值
std::cout << *student_tree; // actual value, and will call operator<<