2016-11-04 73 views
0

我創建一個二叉樹,並試圖打印的即時通訊傳遞當我嘗試打印樹中的學生對象的名字,我得到一個錯誤:沒有名爲成員,BST

tree.h:181:46: error: ‘class samuel::Student’ has no member named ‘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時,它通過學生。這是我的學生類

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 << endlcout << *student_tree << endl這些都給因爲samuel::Student類型該編譯器搜索printInOrder()的錯誤

tree.h:70:9: error: passing ‘const samuel::BSTree’ as ‘this’ argument discards qualifiers [-fpermissive]

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

得到裏面的地址值好像'printInOrder'是'BSTree'的一部分,而不是每個'Student'的。 –

+0

什麼是printInOrder的訪問修飾符?默認情況下它是私人的,所以也許你無法訪問這個私有方法? –

+0

printInOrder是公開的,inOrder是私人的 – Riggy

回答

1

tree.h:181:46: error: ‘class samuel::Student’ has no member named ‘printInOrder’

Node->get_data()返回對象samuel::Student型。根據上面的代碼,它不在那裏。要解決此問題實施方法:

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

student_tree->insert(* new Student());

看起來很可疑。樹按價值包含Student對象。您在堆上創建一個Student對象,解除引用指針並將值複製到樹中。那之後指針丟失了。這會導致內存泄漏問題。

cout << student_tree << endl however this printed a memory address

因爲它被宣佈爲BSTree<Student>* student_tree。這是一個指向樹的指針,所以輸出是正確的,你打印地址。要打印樹值,需要取消指針的引用:cout << *student_tree << endl

+0

感謝您的回覆。我將它更改爲* student_tree,但現在得到錯誤tree.h:70:9:error:將'const samuel :: BSTree '作爲'this'參數丟棄限定符[-fpermissive] out < Riggy

+0

@Riggy這是因爲你爲'const samuel :: BSTree '調用非const'infix'方法。聲明'infix'爲字符串中綴(​​BTNode * Node)const' – Nikita

+0

這樣做的話,我需要重載+操作符以用於中綴,但我知道我不需要這樣做,所以別的東西這裏一定是個問題。 – Riggy

0

延續到其它應答...

and then calling cout << student_tree << endl however this printed a memory address, would anyone also be able to clarify why that happens as well, thanks

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

student_tree是指向BSTree<Student>這意味着它保持BSTree對象的存儲位置(存儲地址),這是未命名的對象在這種情況下。

你必須取消對它的引用,以做*student_tree

std::cout << *student_tree; // actual value, and will call operator<< 
相關問題