2014-12-07 16 views
0

월요일 밤 마감 예정인 프로젝트가 있습니다. 이 프로젝트는 독립 선언문을 "Independence.txt"로 읽고 그 문자열을 빨간색 검은 색 나무로 만드는 Red Black Tree를 구현하는 것입니다. 먼저 Binary Search Tree로 구현하려고 시도하고 있으며 이미 코드를 완성한 상태에서 색상 및 회전을 추가합니다.이진 검색 트리가 문자열로 읽습니까? C++

현재 직면하고있는 문제는 "오류 C2660" 'RBT :: Insert': 함수가 1 개의 인수를 취하지 않음 '및'IntelliSense : 적합하지 않은 변환 함수 '입니다. std : 문자열 "을"RBT :: RBTNode * "가 존재"하고 IntelliSense : 함수 호출에서 너무 적은 인수가 루트를 가리 킵니다. 삽입 (myString);

또한 이진 파일 검색 나무. 내 삽입 방법은 올바른 생각하고, 문제가 주요 방식으로 자리 잡고 있습니다.

덕분에 어떤 도움을 내가 붙어있다.

#include "stdafx.h" 
#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

template<typename T> 
class RBT 
{ 
    struct RBTNode { 
     T data; 
     RBTNode* left; 
     RBTNode* right; 
    }; 

public: 
    RBT(); 
    ~RBT(); 
    void GetNewNode(RBTNode* root, T data); 
    void Insert(RBTNode* root, T data); 
    bool Search(); 
    void Display(); 

}; 


template <typename T> 
void RBT<T>::GetNewNode(RBTNode* root, T data) { 
    RBTNode* newNode = new RBTNode(); 
    newNode->data = data; 
    newNode->left = newNode->right = NULL; 
    return newNode; 
} 

template <typename T> 
void RBT<T>::Insert(RBTNode* root, T data) { 
    if (root == NULL) { 
     root = GetNewNode(data); 
    } 
    else if (data <= root->data) { 
     root->left = Insert(root->left, data); 
    } 
    else { 
     root->right = Insert(root->right, data); 
    } 
    return root; 
} 

template<typename T> 
bool RBT<T>::Search() { 
    if (root == NULL) return false; 
    else if (root->data == data) return true; 
    else if (data <= root->data) return Search(root->left, data); 
    else return Search(root->right, data); 
} 

template<typename T> 
void RBT<T>::Display() { 
    if (root->left != NULL) 
     display(root->left); 
    cout << root->left << endl; 

    if (root->right != NULL) 
     Display(root->right); 
    cout << root->right << endl; 
} 



int main() 
{ 
    RBT<string> root; 
    string myString; 
    ifstream infile; 
    infile.open("Independence.txt"); 
    while (infile) 
    { 
     infile >> myString; 
     root.Insert(myString); 
    } 

    cin.ignore(); 
    return 0; 
} 
+0

RBTNode * newNode = new BstNode(); – schwer

답변

2

Insert 메서드는 2 개의 인수 (삽입 할 루트와 삽입 할 데이터)를 사용합니다. main에, 당신은 단지 1 (데이터)로 그것을 부르고 있습니다.

+0

main에서 메소드 호출에 무엇을 넣을까요? RBTNode * root 또는 단지 루트인가? "오류 C2664 : 'void RBT :: Insert (RBT :: RBTNode *, T)': 'RBT '에서 'RBT :: RBTNode *'로 인수 1을 변환 할 수 없습니다. ' – user2288502

+0

'RBTNode'는 클래스 내부에 있으므로, 클래스 외부에서 호출 할 수있는 추가 버전의'Insert'가 필요합니다. 그러면 현재'Insert'를 적절한 RBTNode로 호출 할 수 있습니다 '. –

+0

이제 오류가 발생합니다. 'RBT :: Insert': 함수 정의를 기존 선언과 일치시킬 수 없습니다. "어떤 아이디어? 그 문제를 연구하고 그것이 무엇을 제공하는지 봅니다. – user2288502

관련 문제