So wie mein erster C++ Programm, das ich einen wenig Binärbaum machen wollte, aber nach dem ersten Wert nach dem root eingeben:this -> m_xy war ein nullptr
Exception geworfen: Zugriffsverletzung schreiben.
dies-> m_left war nullptr.
Meine Testeingabe: abnehmende int-Zahlen.
Mein Code:
#include<iostream>
class BinaryTree
{
public:
BinaryTree *m_left;
int m_key;
BinaryTree *m_right;
void insert(int value)
{
BinaryTree son;
if (value <= m_key)
{
if (m_left == NULL)
*m_left = { NULL, value, NULL }; //Error
else
(*m_left).insert(value);
}
//uniportant stuff ...
}
//unimportant stuff
};
int main()
{
int rootValue(0);
std::cout << "Enter a value for the root: ";
std::cin >> rootValue;
std::cout<<std::endl;
BinaryTree root = { NULL, rootValue, NULL };
int leafValue(1);
std::cout << "Enter a value for the leaf or enter 0 to finish: ";
std::cin >> rootValue;
while (leafValue != 0)
{
root.insert(leafValue);
std::cout << "Enter a value for the leaf or enter 0 to finish: ";
std::cin >> rootValue;
std::cout << std::endl;
}
root.print();
return 0;
}
Es sieht aus wie Sie de-Referenz der Zeiger vor einem Objekt für sie die Zuteilung, sie nicht automatisch erstellt werden. Du wirst sie irgendwo "neu" usw. brauchen. – Niall
ah ja, danke :). Das hat mir schon geholfen! – Bubibob