Link to home
Start Free TrialLog in
Avatar of ris
ris

asked on

compile problem: headers and classes

I am unable to compile this code and I can't find out why.  What I am trying to do is write a symbol table class.  I am a little out of practice with C++, so I probably have screwed up something very minor and as we all know, those are the hardest errors to find...

Anyway, the error I get is in my .cpp file - it says "SymbolTableEntry is not a class name" when I try to declare anything from the class.  If I comment out all the code for the SymbolTableEntry class, it gives me the same error for the SymbolTable code.  For example, I have this chunk of code in the SymbolTable.cpp file:

SymbolTable::SymbolTable()
  {int i;
  TableSize=DefaultTableSize;
  entries=new SymbolTableEntry[TableSize];
  } //end constructor

and when I compile (if I comment out everything above it in the file) I get the error: "SymbolTable is not a class or namespace name" and then the error "declaration terminated incorrectly" at the :: part of the line.

When I was first programming in C++, I got this error because I left out the semi-colon after the class declaration.  The semi-colon is there this time, but I am still unable to compile.

I am using BC++ 5 and here is the code for my header file.

SymbolTable.h:

#ifndef SymbolTableHeader
#define SymbolTableHeader true

#include "SymbolTable.cpp"

#define DefaultTableSize 1024

enum EntryType {integer, boolean, floating, function};

class SymbolTableEntry {
public:
  SymbolTableEntry();
  SymbolTableEntry(char* name,int bytes,EntryType t);

  void remove();

  //these functions return the data properties
  char* name();
  int bytes();
  EntryType type();
  bool inuse();
  bool deleted();

  //debugging functions
  void display();

private:
  char ename[];
  int ebytes
  EntryType etype;
  bool einuse;
  bool edeleted;

}; // end class


class SymbolTable {
public:
  SymbolTable();
  SymbolTable(int tsize);

  int insert(SymbolTableEntry NewEntry);
  int query(char* name);
  SymbolTableEntry retrieve(char* name);
  bool remove(char* name);
  bool modify(char* name, SymbolTableEntry NewEntry);

  int size(); //returns the max length of the table
 
  //debugging functions
  void degub();
  void dump();

private:
  SymbolTableEntry entries[];
  int TableSize;
}; // end class

#endif
ASKER CERTIFIED SOLUTION
Avatar of thresher_shark
thresher_shark

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thresher_shark
thresher_shark

Okay, you are correct, the error is very minor.  When you use the #include statement, you want to include .h files, not .cpp files as you did on this line:

#ifndef SymbolTableHeader
#define SymbolTableHeader true

#include "SymbolTable.cpp"

.

You want to reverse the procedure.  You #include the .h file in the .cpp file! :-)

Here is an example:

You have class x:

Here is the sample .h file:

class x
{ x ();
  ~x ();
};

Sample .cpp file using the x class:

#include "classx.h" // or whatever the name of the class is.
x::x ()
{
}

x::~x ()
{
}

End sample.

Now, whenever you need to use the class, you include the .h file!

Note that it is legal to #include .cpp files, but I have never seen this in any code ever.  What is happening with the include statement is that the code in the .cpp file is being inserted in place of the #include statement.  So, while the compiler is compiling, it encounters the class constructor before it encounters the class declaration.

I hope I have made that all clear... If you have any questions, please ask before "grading" me.  Thanks!
Avatar of ris

ASKER

That helped a lot!  Thanks.

Glad I could help. :-)