C++로 트리 짜고 있는데 삽입 짜고 출력 해보려는데 자꾸 오류 떠요 ㅠ
//BST.h
class BST
{
private:
struct node
{
int key;
node* left;
node* right;
};
node* root;
void AddLeafPrivate(int key, node* ptr);
void PrintInorderPrivate(node* ptr);
public:
BST();
node* CreateLeaf(int key);
void AddLeaf(int key); // Add 함수
void PrintInorder(); // Print 함수
};
======================================================
//BST.cpp
#include <iostream>
#include <cstdlib>
#include "BST.h"
using namespace std;
BST::BST()
{
root = NULL;
}
BST::node* BST::CreateLeaf(int key)
{
node* n = new node;
n->key = key;
n->left = NULL;
n->right = NULL;
n->right = NULL;
return n;
}
void BST::AddLeaf(int key)
{
AddLeafPrivate(key, root);
}
void BST::AddLeafPrivate(int key, node* ptr) //Add
{
if (root == NULL)
{
root = CreateLeaf(key);
}
else if (key > ptr->key) //큰 경우 RightChild로
{
if (ptr->right != NULL)
{
AddLeafPrivate(key, ptr->right);
}
else
{
ptr->right = CreateLeaf(key);
}
}
else
{
cout << "Key" << key << "이미 Tree에 추가 되었습니다." << endl;
}
}
void BST::PrintInorder()
{
PrintInorderPrivate(root);
}
void BST::PrintInorderPrivate(node* ptr)
{
if (root != NULL)
{
if (ptr->left != NULL)
{
PrintInorderPrivate(ptr->left);
}
cout << ptr->key << " ";
if (ptr->right != NULL)
{
PrintInorderPrivate(ptr->right);
}
}
else
{
cout << "Tree가 비었습니다" << endl;
}
}
===================================================
//main.cpp
#include <iostream>
#include <cstdlib>
#include "BST.cpp"
using namespace std;
int main()
{
int TreeKeys[16] = { 50, 76, 21, 4, 32, 64, 15, 52, 14, 100, 83, 2, 3, 70, 87, 80};
BST myTree;
cout << "Print Tree in order\nBefore adding numbers" << endl;
myTree.PrintInorder();
for (int i = 0; i < 16; i++)
{
myTree.AddLeaf(TreeKeys[i]);
}
cout << "Printing the tree in order\nAfter adding numbers" << endl;
myTree.PrintInorder();
return 0;
}
========================================================
// 출력 결과
1>------ 빌드 시작: 프로젝트: Tree, 구성: Debug Win32 ------
1> main.cpp
1>main.obj : error LNK2005: "public: __thiscall BST::BST(void)" (??0BST@@QAE@XZ)이(가) BST.obj에 이미 정의되어 있습니다.
1>main.obj : error LNK2005: "public: void __thiscall BST::AddLeaf(int)" (?AddLeaf@BST@@QAEXH@Z)이(가) BST.obj에 이미 정의되어 있습니다.
1>main.obj : error LNK2005: "private: void __thiscall BST::AddLeafPrivate(int,struct BST::node *)" (?AddLeafPrivate@BST@@AAEXHPAUnode@1@@Z)이(가) BST.obj에 이미 정의되어 있습니다.
1>main.obj : error LNK2005: "public: struct BST::node * __thiscall BST::CreateLeaf(int)" (?CreateLeaf@BST@@QAEPAUnode@1@H@Z)이(가) BST.obj에 이미 정의되어 있습니다.
1>main.obj : error LNK2005: "public: void __thiscall BST::PrintInorder(void)" (?PrintInorder@BST@@QAEXXZ)이(가) BST.obj에 이미 정의되어 있습니다.
1>main.obj : error LNK2005: "private: void __thiscall BST::PrintInorderPrivate(struct BST::node *)" (?PrintInorderPrivate@BST@@AAEXPAUnode@1@@Z)이(가) BST.obj에 이미 정의되어 있습니다.
1>C:\Users\Jo\Desktop\host\coding\Tree\Debug\Tree.exe : fatal error LNK1169: 여러 번 정의된 기호가 있습니다.
문제점좀 알려주세요 형들 ㅠ ㅠ
왜 cpp를 인클루드함? 헤더를 해야지
main에서
bst.h / bst.cpp / main.cpp 파일 세개구요 형 bst.cpp가 헤더 인클하고 메인이 bst.cpp 인클하게 맹글어 놨어욤
h, cpp 둘다 인클루드 할필요 없고 h만 하면 됨
감사합니다 형