2013-11-26 2 views
0

다른 파일에있는 클래스 BST에서 'bst'객체를 선언하려고합니다. 나는 그것을 작동시키는 데 어려움을 겪고있다. 지금까지이 파일을 컴파일하려고하면 오류 메시지가 나타납니다. 다른 파일에있는 객체 선언하기

$ make -f makefile.txt 
g++ -Wall -W -Werror -pedantic -g -c BSTapp.cpp 
BSTapp.cpp: In function `int main()': 
BSTapp.cpp:9: error: `BST' undeclared (first use this function) 
BSTapp.cpp:9: error: (Each undeclared identifier is reported only once for each function it appears in.) 
BSTapp.cpp:9: error: expected `;' before "bst" 
makefile.txt:5: recipe for target `BSTapp.o' failed 
make: *** [BSTapp.o] Error 1 

함께 .... BST.h (주)

#include "BSTapp.h" 
#include <iostream> 
#include <string> 
#include <cstring> 
#include <algorithm> 
using namespace std; 
int main() 
{ 
    BST bst; //<<this is where I am trying to declare the object. 
    cout << "hi" << endl; 
    cout << "in main" << endl; 
    string line; 
    string command = "aaaa"; 
    string strKey; 
    string data; 
    //char ignore[] = "/"; 
    while(command != "quit") 
    { 
     cout << "in while loop" << endl; 
     cin >> command; 
     cout << "Command is:" << command << endl; 
     if(command == "insert") 
     { 
      cin >> strKey; 
      strKey.erase(2, 1); 
      int intKey = atoi(strKey.c_str()); 
      cout << intKey << endl; 
      cin.ignore(); 
      getline(cin, data); 
      cout << data << endl; 
     } 
     if(command == "find") 
     { 

     } 
     if(command == "delete") 
     { 

     } 
     if(command == "print") 
     { 

     } 
    } 
    return 0; 
} 

#ifndef BST_H_INCLUDED 
#define BST_H_INCLUDED 

#include <iostream> 
#include <string> 

using namespace std; 

class BST 
{ 
public: 
    BST(); 
    ~BST(); 
    void insert(int key, string data); 
    void find(int key); 
    void remove(int key, string data); 
    void print(); 
    friend class Node; 
private: 
    Node* m_root; 

}; 

#endif // BST_H_INCLUDED 

bst.cpp

#include "BST.h" 

void BST::insert(int key, string data) 
{ 

} 

bstapp.cpp 시작하는 파일입니다

BSTapp.h

#ifndef BSTAPP_H_INCLUDED 
#define BSTAPP_H_INCLUDED 

#include <string> 
#include <iostream> 

using namespace std; 

/*class NodeData 
{ 
public: 
    NodeData(int key, string data) 
    {m_key = key; m_data = data;} 
    //~NodeData(); // add this in eventually 
private: 
    int m_key; 
    string m_data; 

};*/ 
class BSTapp 
{ 
public: 
private: 
}; 

#endif // BSTAPP_H_INCLUDED 

친구 노드가이 같은 선언 ...

#ifndef NODE_H_INCLUDED 
    #define NODE_H_INCLUDED 

    #include <iostream> 
    #include <string> 

    using namespace std; 
class Node 
{ 
public: 
    Node(int key, string data) 
    {m_key = key; m_data = data;} 
    ~Node(); 
    //friend BST(); 
private: 
    int m_key; 
    string m_data; 
    Node *m_left; 
    Node *m_right; 
    //Node *m_parent; 
}; 


#endif // NODE_H_INCLUDED 

는 기본적으로, 난 그냥 내가 할당이 저를 필요로 (BST에 새로운 노드를 구성 할 수 있습니다 INT 주에서 객체를 선언 할 이 모든 파일을 사용하여 모든 것을 .h와 .cpp에 넣으십시오. 6이되어야합니다.). 다시 말하지만, 메인에 객체를 선언하는 방법을 찾는 데 어려움을 겪고 있습니다. 필요한 정보가 있으면 남겨주세요. 정말이 사이트에서 질문하는 것이 좋습니다.

+0

bst.h를 bstapp.cpp에 포함시키지 않습니다. – Domi

+0

나는 허용되지 않습니다. 강사는 짝을 포함 할 수 있습니다. –

+0

[헤더 파일에 "using namespace"를 추가하지 마십시오] (http://stackoverflow.com/questions/5849457/using-namespace-in-c-headers) . 그리고 이름과 들여 쓰기가 일관성이 있는지 확인하십시오. – Domi

답변

0

BSTapp는 BST의 사용자이므로 포함해야합니다. 다른 방법은 없습니다.

+0

내가 그렇게하면 과제가 실패합니다. 강사는 쌍 외부에 아무것도 포함 할 수 없다고 엄격히 말합니다. –

+0

문제를 올바르게 이해했는지 확인하십시오. main() 함수를 포함하는 파일은 대개 파일 쌍으로 선언 된 객체의 사용자이므로 해당 헤더 파일을 포함합니다. 내가 말했듯이, 다른 방법은 없습니다. – Domi

+0

이것은 지침서에 기록 된 내용입니다 ... 작은 인라인 기능 (2 행 최대)을 제외하고 .h 파일의 코드가 없습니다. .h의 파일 쌍 클래스 선언, .cpp 또는 .cc 또는 .C++의 함수 정의. 파일 이름은 클래스 이름과 일치해야합니다. –

관련 문제