我想從另一個文件中的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
#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(主)
#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;
}
文件
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
基本上,我只是想申報INT主要對象,所以我可以在BST構造一個新的節點(分配需要我使用所有這些文件,所以我不能把所有東西都放到.h和.cpp中,它必須是6)。再次,我很難找到如何在main中聲明一個對象。請告訴我,如果我留下任何信息,你需要,我真的很難在這個網站上提出問題。
我沒有看到你在bstapp.cpp中包含BST.h。 – Domi
我不被允許。教師只允許包含在他們的對.. –
[不要添加「使用名稱空間」頭文件](http://stackoverflow.com/questions/5849457/using-namespace-in-c-headers) 。並確保你的名字和縮進是一致的。 – Domi