我试图从另一个文件中的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 main中声明一个对象,这样我就可以在BST中构造一个新的Node(赋值需要我使用所有这些文件,所以我不能把所有内容放到.h和.cpp中,它必须是6)。同样,我无法找到如何在main中声明一个对象。请告诉我,如果我遗漏了你需要的任何信息,我在这个网站上提问时我真的很难。
答案 0 :(得分:0)
BSTapp是BST的用户,因此必须包含它。没有其他办法。