0

I am trying to declare the object 'bst' from the class BST that is in another FILE. I am having trouble getting it to work. So far this is the error message when I try to compile the files.

$ 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

these are the files starting with.... 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 (main)

#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

the friend Node is declared as this...

    #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

basically, I just want to declare an object in int main so I can construct a new Node in BST (the assignment requires me to use all of these files so i cant just put everything into a .h and a .cpp, it has to be 6). Again, I am having trouble finding out how to declare an object in main. please tell me if i left any information out of this that you need, I am really bad at asking questions on this site.

Ryan Mann
  • 53
  • 1
  • 10
  • I don't see you include BST.h in bstapp.cpp. – Domi Nov 26 '13 at 02:56
  • I'm not allowed to. the instructor only allows the includes to be in their pairs.. – Ryan Mann Nov 26 '13 at 02:57
  • [Don't add "using namespace" to header files](http://stackoverflow.com/questions/5849457/using-namespace-in-c-headers). And make sure your names and indentation are consistent. – Domi Nov 26 '13 at 02:59
  • I did that just now, good to know, but I'm confused on how I could declare an object in this situation. – Ryan Mann Nov 26 '13 at 03:00

1 Answers1

0

BSTapp is a user of BST, and thus must include it. There is no other way.

Domi
  • 22,151
  • 15
  • 92
  • 122
  • I'll fail the assignment if I do that, the instructor strictly states that we are not allowed to include anything outside of the pairs. – Ryan Mann Nov 26 '13 at 03:02
  • Make sure that you understand the problem correctly. The file containing the main() function is usually the user of objects that are declared in pairs of files, and thus includes their header files. As I said, there is no other way. – Domi Nov 26 '13 at 03:05
  • this is what was noted in the instructions tho... No code in .h files except for small inline functions (two lines max). One class per file pair - class declaration in .h, function definitions in .cpp or .cc or .c++ . The file names should match the class name. – Ryan Mann Nov 26 '13 at 03:08
  • The main() function is not a class. – Domi Nov 26 '13 at 03:20