GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Mar-2008, 02:40
pfanning pfanning is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 39
pfanning is on a distinguished road

bool problem in multi file format


Hi,

I am using multi files on this assignment with code assigned to be used "as is". Problem is the utility file is giving an error on the bool line in the form of a compile error that says 'bool : redefinition; different basic types' and to look at the definition of bool in Lab2.cpp. I am at a loss to see why this is not working, except that it probably has something to do with 'const'. However, 'const' is a required part of this exercise...

Code follows, grateful for any advice you can offer so that I can fix the error(s)...
CPP / C++ / C Code:
//The utlitiy file is giving me the headahce.  Specifically,
//the line containing the bool statement.  

//Begin utility file:
enum bool {false, true};
enum Error_code {success, overflow, underflow};

#endif

//end utility file

//Lab2.cpp this file's purpose is to act as a simple test driver
#include <iostream>
#include "utility.h"
#include "Queue.h"

using namespace std;

main()
{
	Queue aQueue;
}
//end file Lab2.cpp

// File Queue.h begins here

#ifndef QUEUE_H
#define QUEUE_H

const int maxqueue = 10;
typedef char Queue_entry;

class Queue{
public:
	Queue();
	bool empty()const;
	Error_code serve();
	Error_code append(const Queue_entry &item);
	Error_code retrieve(Queue_entry &item)const;
private:
	int count;
	int front, rear;
	Queue_entry entry[maxqueue];
};

#endif
//end file Queue.h

//File Queue.cpp begins here:

#include "Queue.h"

Queue::Queue()
/*Post: The Queue is initialized to be empty.*/
{
	count = 0;
	rear = maxqueue-1;
	front = 0;
}

bool Queue::empty()  //*******This is the only instance of bool in this file
/*Post: Return true if the Queue is empty, otherwise return false. */
{
	return count == 0;
}

Error_code Queue::append(const Queue_entry &item)
/*Post: item is added to the rear of the Queue.  If the Queue is full return an
		Error_code of overflow and leave the Queue unchanged*/
{
	if(count>=maxqueue) return overflow;
	count++;
	rear =((rear + 1) == maxqueue)? 0 : (rear +1);
	entry[rear] = item;
	return success;
}

Error_code Queue::serve()
/*Post: The front of the Queue is removed.  If the Queue is empty, return an
		Error_code of underflow.*/
{
	if(count<=0) return underflow;
	count--;
	front=((front+1) == maxqueue) ? 0 : (front+1);
	return success;
}

Error_code Queue::retrieve(Queue_entry &item)const
/*Post: The front of the Queue retrieved to the output parameter item.  If the
		Queue is empty return an Error_code of underflow.*/
{
	if(count<=0) return underflow;
	item = entry[front];
	return success;
}
//end of file Queue.cpp
  #2  
Old 03-Mar-2008, 09:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: bool problem in multi file format


Quote:
Originally Posted by pfanning
...to be used "as is"...

Other than something missing at the beginning of your Utility.h (probably some #ifndef...#define stuff for the inclusion guards), there are a couple of problems that make the assignment quite impossible to complete if you are using a standard C++ compiler.

First of all, bool is a reserved key word in standard C++. bool is a built-in data type, and, in the exact words of the C++ standard: bool will be "unconditionally treated as" a keyword. See Footnote.

Now, if you comment out that line in Util.h, you are left with an error that the prototype for Queue::empty is different from the implementation definition. One has const and the other doesn't. They must agree. Exactly.

Also, standard C++ forbids declaration of 'main' with no type (it has to have an integer data type, usually int). Some compilers allow it as you have shown, but it's not really right.

Finally, in my opinion, the file Queue.h requires that Utility.h be included. The fact that you put them in the correct order in your main file makes it work, but I think that it is bad program design. I really, really believe that all files that require inclusion of some other file should #include them explicitly, and not depend on some other program unit including them in the correct order.

I understand that you may not be in a position to challenge whoever it was that gave you the stuff with the command to use them "as is," but I thought it worth a comment or two.

Bottom line: It won't work on any of the various compilers that I have unless you throw away the bool redefinition and make the empty definition agree with its prototype.

Regards,

Dave

Footnote:
Reference International Standard ISO/IEC 14882-2003 Programming languages --- C++
  #3  
Old 03-Mar-2008, 09:45
pfanning pfanning is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 39
pfanning is on a distinguished road

Re: bool problem in multi file format


Thanks Dave. I don't know if you remember, but you were extremely helpful on an inventory program I had to complete for my class last semester. Thanks again.

I have this class and lab tonight. I am thinking the instructor left something out (or I missed something). I'll see what I can find out and thank you in the meantime for your help.

Best regards,

Paul
  #4  
Old 03-Mar-2008, 10:00
pfanning pfanning is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 39
pfanning is on a distinguished road

Re: bool problem in multi file format


Ahhh! That was it!! Including utility.h in Queue.h leads to a successful compile! Eureka!!

That #include in Queue.h is fine with respect to the rules of the assignment. I was not entirely clear about that in my first post. The instructor is really picky about having code input exactly as it appears in his assignment. However, there are exceptions. For instance, there are no restrictions on using #include "utility.h". On the other hand, the class and functions must appear "as is." He has a main program too that must appear precisely as he has assigned it. I hope you will bear with me as I may have new questions as I add in the stack class, its functions, and main().

Thank you Dave! You are the best!

Paul Fanning
  #5  
Old 08-Mar-2008, 17:07
pfanning pfanning is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 39
pfanning is on a distinguished road

Re: bool problem in multi file format


I spoke too soon. I had remarked out the bool line in ulility.h which is why it was compiling. I got deeper into the project before I discovered the error. It's the same problem, "redefinition; different basic types" and "syntax error constant". There are also two errors about "missing ; before {" and "missing ; before }" but I believe those will clear up. The only lines currently in use for bool are "bool empty()const" in Queue.h and Queue.cpp. If you have ideas as to how I can solve, please adivse.
  #6  
Old 08-Mar-2008, 17:35
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: bool problem in multi file format


Quote:
Originally Posted by pfanning
I spoke too soon. I had remarked out the bool line in ulility.h which is why it was compiling. I got deeper into the project before I discovered the error. It's the same problem, "redefinition; different basic types" and "syntax error constant". There are also two errors about "missing ; before {" and "missing ; before }" but I believe those will clear up. The only lines currently in use for bool are "bool empty()const" in Queue.h and Queue.cpp. If you have ideas as to how I can solve, please adivse.

I have already told you:
Quote:
Originally Posted by davekw7x

First of all, bool is a reserved key word in standard C++. bool is a built-in data type, and, in the exact words of the C++ standard: bool will be "unconditionally treated as" a keyword.

Reserved means that you can't define it to be something else. Period. If you or your instructor was using some really old (pre-Standard) C++ compiler, maybe re-defining bool worked, but it's just wrong, wrong, wrong.

Get rid of the enum bool thing and try again. If you still have problems, post whatever it is that you are currently working with.

Regards,

Dave
  #7  
Old 08-Mar-2008, 17:45
pfanning pfanning is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 39
pfanning is on a distinguished road

Re: bool problem in multi file format


Ok. I noticed that the deliverables for this assignment do not include the utility.h file. So, I'm going to move on without the enum bool line. No sense in banging my head against the wall if it is not required.

Thanks again Dave.
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Power Calibration Error In Nero Fix (hopefully) matt3678 Computer Hardware Forum 53 30-Jun-2008 14:19
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
need help with a console menu system BullBuchanan CPP / C++ Forum 6 20-Aug-2006 14:46
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 20:30
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.