バイナリ検索ツリーを作成しようとしています。私は、挿入関数に取り組んでいますが、私はいくつかの互換性のない型の警告を取得しています。BSTNode *からBSTNodeへの割り当てに互換性のない型*
warning C4133: '=' : incompatible types - from 'BSTNode *' to 'BSTNode *'
私のコードの22行目、25行目、36行目、36行目にこれらの警告が表示されています。私はまた、私の2つの再帰呼び出しで警告warning C4133: 'function' : incompatible types - from 'BSTNode *' to 'BSTNode *'
を取得しています。私は以下のコードでコメントにコメントを付けました。これらは同じタイプなので、私はこれらの警告の原因を突き止めることはできません。メンバーを定義しながら、BSTNode
がある、ので、あなたのヘッダファイルに
//BST.c
#include "BST.h"
#include <stdio.h>
#include <stdlib.h>
void insert(BSTNode* top_of_bst,BSTNode* node){
//no items in bst, add it to the top
if (top_of_bst == NULL){
top_of_bst->node_value = node->node_value;
top_of_bst->left = NULL;
top_of_bst->right = NULL;
top_of_bst->parent = NULL;
return;
}
//if the value is smaller check the left child
if (top_of_bst->node_value >= node->node_value){
if (top_of_bst->left == NULL){
node->parent = top_of_bst; //HERE IS AN ERROR
node->right = NULL;
node->left = NULL;
top_of_bst->left = node; //HERE IS AN ERROR
return;
}
//if the left child exists, recurse left
else
insert(top_of_bst->left, node); //HERE IS AN ERROR
}
//if the value is bigger check the right child
else{
if (top_of_bst->right == NULL){
top_of_bst->right = node; //HERE IS AN ERROR
node->parent = top_of_bst; //HERE IS AN ERROR
node->left = NULL;
node->right = NULL;
return;
}
//if the child exists, recurse right
else
insert(top_of_bst->right, node); //HERE IS AN ERROR
}
}
そして、これが私のBstNodeヘッダファイルです
#ifndef BSTNODE_H
#define BSTNODE_H
typedef struct BSTNODE{
struct BSTNode* parent;
struct BSTNode* left;
struct BSTNode* right;
int node_value;
}BSTNode;
#endif
'struct BSTNode * 'の代わりに' struct BSTNODE * 'を使用してください。 typedef'edの名前は構造体定義の中でまだ使用できません。もしそれがあったとしても、その前に 'struct'は必要ありません。 – Dmitri
どの行が正確に表示されますか?行番号は表示されませんでした。また、 'top_of_bst'がNULLの場合、新しい' BSTNode'を最初に作成していないので、コードにメンバーへのアクセスがクラッシュすることになります。 –