Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

compile problem: headers and classes

Posted on 1998-09-03
4
Medium Priority
?
145 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 6

Accepted Solution

by:
thresher_shark earned 800 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

688 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