nullptrた:は、この - > m_xyは、だから私は少しバイナリツリーを作りたかったのですが、ルートの後の最初の値を入力した後、私の最初のC++プログラムとして
例外がスローさ:アクセス違反の書き込み。
this-> m_leftはnullptrです。
私のテスト入力:int値の減少。
マイコード:
#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;
}
それが彼らのためにオブジェクトを割り当てる前に、あなたのようなデリファレンスポインタを調べ、それらが自動的に作成されていません。あなたはどこかでそれらを「新しくする」必要があります。 – Niall
ああええ、thx :)これは私を全部手伝った! – Bubibob