Solved

compile problem: headers and classes

Posted on 1998-09-03
4
136 Views
Last Modified: 2010-07-27
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
0
Comment
Question by:ris
  • 3
4 Comments
 
LVL 6

Accepted Solution

by:
thresher_shark earned 200 total points
ID: 1172017
Answer coming...
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1172018
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!
0
 
LVL 1

Author Comment

by:ris
ID: 1172019
That helped a lot!  Thanks.

0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1172020
Glad I could help. :-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now