PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > The elevator simulation
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
The elevator simulation

Réponse
 
LinkBack Outils de la discussion
Vieux 15/10/2007, 18h56   #1
whitehatmiracle@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut The elevator simulation

Hi all...
Im getting some errors, and have no clue, as to what could be wrong...

SOS... anybody ??
heres the code:

// The elevator simulation

//4 errors of 2 of same type
//error, type qualifier 'elevator' must be a struct or clas name
// error declaration terminated incorrectly

#include "include.h"

class button
{
enum button_status {off, on};
button_status status;
public:
button(button_status = off);
void set_state(button_status);
button_status get_state();

};

inline void button::set_state(button_status n){
status= n;
}

inline button::button_status button::get_state() {
return status;
}

//error, type qualifier 'elevator' must be a struct or clas name
// error declaration terminated incorrectly
elevator::button *b(int n) : button_array(0), number(n)
{
button_array= new button*[number + 1];
for(int i = 0; i < number + 1; ++i)
button_array[i] = new button;
}

elevator::~elevator()
{
for(int i = number; i >= 0; --i)
delete button_array[i];
delete [] button_array;
}


class elevator
{
public:
elevator(int = 1, int = 0);
~elevator();
void prompt();
private:
button *b;
int current_floor;
const int top_floor;
bool button_active(int) const;
bool is_off (int n); // return button pressed as off
bool is_on (int n); /// return button preesed for floor
void press(int);
void reset(int n); returns off state
void close_doors();
bool floor_is_valid(int) const; //// bool ->enum
elevator(const elevator&);
};

// type qualifier 'elevator' must be a struct or class name
//declaration terminated incorrectly

elevator::elevator(int n, int w) : buttons(n), current_floor(1),
top_floor(n) {}

elevator::~elevator()
{
cout << "Elevator will self destruct\n";
}

void elevator::prompt()
{
cout << "Key in the floor you would like to visit, from 1 to " <<
top_floor << ", 0 to close doors, EOF to exit: ";
int floor;
while ( !(cin >> floor).eof())
{
if(floor == 0)
{
if(!button_active(1) && current_floor != 1)
press(1);
if(button_active(1))
close_doors();
}
else if(floor < 1 || floor > top_floor)
cout << "***Floor " << floor << " is not valid\n";
else if(floor == current_floor)
cout << "***You have reached your floor now\n";
else
press(floor);
cout << "Next floor: ";
}
}

void elevator::press(int n)
{
buttons.press(n);
}

void elevator::press(int n)
{
if(!is_valid(n))
cout << "Wrong entry of floor!\n";
else
button[n]->is_on();
}



void elevator::close_doors()
{
cout << "Doors are closing\n";
cout << "\a\a\a\a\a";
// If a button is pushed on a floor higher than the current floor,
then the elevator //always moves up.
if(button_active(current_floor))
cout << "Elevator accending\n";
else
cout << "Elevator decending\n";
sleep(1);
// Keep looping until floor buton is off
while(buttons.is_off(current_floor))
{
if(button_active(current_floor))
++current_floor;
else
--current_floor;
if(buttons.is_off(current_floor) && floor_is_valid(current_floor))
{
cout << "\tPassing floor " << current_floor << '\a' << '\n';
sleep(1);
}
}
cout << "\tNow on floor " << current_floor << "\a\a\a" << '\n';
sleep(1);
buttons.reset(current_floor); //reset is to put button off
cout << "Doors are open\n";
sleep(1);
}

bool elevator::button_active(int f) const
{
for(int i = f; i <= top_floor; ++i)
if(buttons.is_on(i))
return true;
return false;
}

bool elevator::floor_is_valid(int q) const
{
return buttons.is_valid(q);
}

int main()
{
const int top = 9;
elevator otis(top);
otis.prompt();
return 0;
}

  Réponse avec citation
Vieux 15/10/2007, 19h33   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: The elevator simulation

whitehatmiracle@gmail.com wrote:
> Hi all...
> Im getting some errors, and have no clue, as to what could be wrong...
>
> SOS... anybody ??
> heres the code:
>
> [..]
> //error, type qualifier 'elevator' must be a struct or clas name
> // error declaration terminated incorrectly
> elevator::button *b(int n) : button_array(0), number(n)
> {
> button_array= new button*[number + 1];
> for(int i = 0; i < number + 1; ++i)
> button_array[i] = new button;
> }
>
> elevator::~elevator()
> {
> for(int i = number; i >= 0; --i)
> delete button_array[i];
> delete [] button_array;
> }


What are those two "functions" supposed to be for? If they are
definitions of the member functions of the 'elevator' class, they are
processed by the compiler before the class. If they are not members,
what are they? Also, you seem to have two different versions of the
'elevator' destructor in your code. You need to pick one (and remove
the other).

>
>
> class elevator
> {
> [..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 15/10/2007, 19h52   #3
Erik Wikström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: The elevator simulation

On 2007-10-15 19:56, whitehatmiracle@gmail.com wrote:
> Hi all...
> Im getting some errors, and have no clue, as to what could be wrong...
>
> SOS... anybody ??
> heres the code:
>
> // The elevator simulation
>
> //4 errors of 2 of same type
> //error, type qualifier 'elevator' must be a struct or clas name
> // error declaration terminated incorrectly


Actually you have a lot more errors than that, for some of them I can
not even guess what the correct solution should be.

> #include "include.h"


What is include.h? We have not seen this file so if it is necessary for
your program to work you should post it too. If it is not necessary you
should not include it.

> class button
> {
> enum button_status {off, on};
> button_status status;
> public:
> button(button_status = off);
> void set_state(button_status);
> button_status get_state();
>
> };
>
> inline void button::set_state(button_status n){
> status= n;
> }
>
> inline button::button_status button::get_state() {
> return status;
> }
>
> //error, type qualifier 'elevator' must be a struct or clas name
> // error declaration terminated incorrectly
> elevator::button *b(int n) : button_array(0), number(n)
> {
> button_array= new button*[number + 1];
> for(int i = 0; i < number + 1; ++i)
> button_array[i] = new button;
> }
>


This is a fun one, it is a function named b, which takes an int as an
argument. Since it has an initialisation-list it should be a constructor
but since it have a return-type it is not. The return-type is a pointer
to an object of type elevator::button, where elevator is a class, struct
or a namespace?

To be frank I have absolutely no idea what that function is supposed to
be, I can not find anything like it in the elevator class declared
below, or in the button class above.


> elevator::~elevator()
> {
> for(int i = number; i >= 0; --i)
> delete button_array[i];
> delete [] button_array;
> }


Move this below the class definition. BTW: there is no member named
either number or button_array in the elevator class.

> class elevator
> {
> public:
> elevator(int = 1, int = 0);
> ~elevator();
> void prompt();
> private:
> button *b;
> int current_floor;
> const int top_floor;
> bool button_active(int) const;
> bool is_off (int n); // return button pressed as off
> bool is_on (int n); /// return button preesed for floor


What is the difference between the above three functions?

> void press(int);
> void reset(int n); returns off state


You forgot to add // at the beginning of the comment.

> void close_doors();


How do you open the doors? And how do you know if they are open or closed?

> bool floor_is_valid(int) const; //// bool ->enum
> elevator(const elevator&);
> };
>
> // type qualifier 'elevator' must be a struct or class name
> //declaration terminated incorrectly
>
> elevator::elevator(int n, int w) : buttons(n), current_floor(1),
> top_floor(n) {}
>
> elevator::~elevator()
> {
> cout << "Elevator will self destruct\n";
> }
>
> void elevator::prompt()
> {
> cout << "Key in the floor you would like to visit, from 1 to " <<
> top_floor << ", 0 to close doors, EOF to exit: ";
> int floor;
> while ( !(cin >> floor).eof())
> {
> if(floor == 0)
> {
> if(!button_active(1) && current_floor != 1)
> press(1);
> if(button_active(1))
> close_doors();
> }
> else if(floor < 1 || floor > top_floor)
> cout << "***Floor " << floor << " is not valid\n";
> else if(floor == current_floor)
> cout << "***You have reached your floor now\n";
> else
> press(floor);
> cout << "Next floor: ";
> }
> }
>
> void elevator::press(int n)
> {
> buttons.press(n);
> }
>
> void elevator::press(int n)
> {
> if(!is_valid(n))
> cout << "Wrong entry of floor!\n";
> else
> button[n]->is_on();
> }


That is twice that you have defined elevator::press().

> void elevator::close_doors()
> {
> cout << "Doors are closing\n";
> cout << "\a\a\a\a\a";
> // If a button is pushed on a floor higher than the current floor,
> then the elevator //always moves up.
> if(button_active(current_floor))
> cout << "Elevator accending\n";
> else
> cout << "Elevator decending\n";
> sleep(1);
> // Keep looping until floor buton is off
> while(buttons.is_off(current_floor))
> {
> if(button_active(current_floor))
> ++current_floor;
> else
> --current_floor;
> if(buttons.is_off(current_floor) && floor_is_valid(current_floor))
> {
> cout << "\tPassing floor " << current_floor << '\a' << '\n';
> sleep(1);
> }
> }
> cout << "\tNow on floor " << current_floor << "\a\a\a" << '\n';
> sleep(1);
> buttons.reset(current_floor); //reset is to put button off
> cout << "Doors are open\n";
> sleep(1);
> }
>
> bool elevator::button_active(int f) const
> {
> for(int i = f; i <= top_floor; ++i)
> if(buttons.is_on(i))
> return true;
> return false;
> }
>
> bool elevator::floor_is_valid(int q) const
> {
> return buttons.is_valid(q);
> }
>
> int main()
> {
> const int top = 9;
> elevator otis(top);
> otis.prompt();
> return 0;
> }


Somehow I just do not believe that the code above is the one you are
trying to compile. Please read the FAQ on how to ask questions and try
again: http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

If the above code really is the code you are trying to compile then do this:
1. Remove all code except the code for the button class.
2. Write the definition for the elevator class, considering carefully
what information you need to store and which functions you need.
3. Compile.
4. Fix any compilation errors.
5. Implement the elevator's constructor.
6. Compile.
7. Fix any compilation errors.
8. Implement one of the elevator's functions.
7. Compile.
8. Fix any compilation errors.
9. If all functions are implemented go to 10, else go to 8.
10. Write main().
11. Compile.
12. Fix any compilation errors.

--
Erik Wikström
  Réponse avec citation
Vieux 15/10/2007, 20h19   #4
Shadowman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: The elevator simulation

Erik Wikström wrote:

> Somehow I just do not believe that the code above is the one you are
> trying to compile. Please read the FAQ on how to ask questions and try
> again: http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
>
> If the above code really is the code you are trying to compile then do this:
> 1. Remove all code except the code for the button class.
> 2. Write the definition for the elevator class, considering carefully
> what information you need to store and which functions you need.
> 3. Compile.
> 4. Fix any compilation errors.
> 5. Implement the elevator's constructor.
> 6. Compile.
> 7. Fix any compilation errors.
> 8. Implement one of the elevator's functions.
> 7. Compile.
> 8. Fix any compilation errors.
> 9. If all functions are implemented go to 10, else go to 8.
> 10. Write main().
> 11. Compile.
> 12. Fix any compilation errors.
>


That's one hell of a numbering system

--
SM
rot13 for email
  Réponse avec citation
Vieux 15/10/2007, 20h33   #5
Erik Wikström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: The elevator simulation

On 2007-10-15 21:19, Shadowman wrote:
> Erik Wikström wrote:
>
>> Somehow I just do not believe that the code above is the one you are
>> trying to compile. Please read the FAQ on how to ask questions and try
>> again: http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
>>
>> If the above code really is the code you are trying to compile then do this:
>> 1. Remove all code except the code for the button class.
>> 2. Write the definition for the elevator class, considering carefully
>> what information you need to store and which functions you need.
>> 3. Compile.
>> 4. Fix any compilation errors.
>> 5. Implement the elevator's constructor.
>> 6. Compile.
>> 7. Fix any compilation errors.
>> 8. Implement one of the elevator's functions.
>> 7. Compile.
>> 8. Fix any compilation errors.
>> 9. If all functions are implemented go to 10, else go to 8.
>> 10. Write main().
>> 11. Compile.
>> 12. Fix any compilation errors.
>>

>
> That's one hell of a numbering system


I think I ate to much copy-pasta for supper. At least this way the OP
has an option of which 8 to go to. :-)

--
Erik Wikström
  Réponse avec citation
Vieux 15/10/2007, 21h08   #6
osmium
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: The elevator simulation

<whitehatmiracle@gmail.com> wrote:


> Im getting some errors, and have no clue, as to what could be wrong...
>
> SOS... anybody ??
> heres the code:
>
> // The elevator simulation
>
> //4 errors of 2 of same type
> //error, type qualifier 'elevator' must be a struct or clas name
> // error declaration terminated incorrectly
>
> #include "include.h"
>
> class button
> {
> enum button_status {off, on};
> button_status status;
> public:
> button(button_status = off);
> void set_state(button_status);
> button_status get_state();
>
> };
>
> inline void button::set_state(button_status n){
> status= n;
> }
>
> inline button::button_status button::get_state() {
> return status;
> }
>
> //error, type qualifier 'elevator' must be a struct or clas name
> // error declaration terminated incorrectly


Move this function to below the definiton of elevator
elevators don't yet exist. Read top to bottom, that's the way the compiler
does it.

> elevator::button *b(int n) : button_array(0), number(n)
> {
> button_array= new button*[number + 1];
> for(int i = 0; i < number + 1; ++i)
> button_array[i] = new button;
> }
>

move this function to below the definition of elevator. You are trying to
destroy something which doesn't yet exist.

> elevator::~elevator()
> {
> for(int i = number; i >= 0; --i)
> delete button_array[i];
> delete [] button_array;
> }
>
>
> class elevator
> {
> public:
> elevator(int = 1, int = 0);
> ~elevator();
> void prompt();
> private:
> button *b;
> int current_floor;
> const int top_floor;
> bool button_active(int) const;
> bool is_off (int n); // return button pressed as off
> bool is_on (int n); /// return button preesed for floor
> void press(int);
> void reset(int n); returns off state
> void close_doors();
> bool floor_is_valid(int) const; //// bool ->enum
> elevator(const elevator&);
> };
>
> // type qualifier 'elevator' must be a struct or class name
> //declaration terminated incorrectly
>
> elevator::elevator(int n, int w) : buttons(n), current_floor(1),
> top_floor(n) {


if you want your elevator to have buttons, this is probably the place to
generate them.

}
>
> elevator::~elevator()
> {
> cout << "Elevator will self destruct\n";
> }
>
> void elevator::prompt()
> {
> cout << "Key in the floor you would like to visit, from 1 to " <<
> top_floor << ", 0 to close doors, EOF to exit: ";
> int floor;
> while ( !(cin >> floor).eof())
> {
> if(floor == 0)
> {
> if(!button_active(1) && current_floor != 1)
> press(1);
> if(button_active(1))
> close_doors();
> }
> else if(floor < 1 || floor > top_floor)
> cout << "***Floor " << floor << " is not valid\n";
> else if(floor == current_floor)
> cout << "***You have reached your floor now\n";
> else
> press(floor);
> cout << "Next floor: ";
> }
> }
>
> void elevator::press(int n)
> {
> buttons.press(n);
> }
>
> void elevator::press(int n)
> {
> if(!is_valid(n))
> cout << "Wrong entry of floor!\n";
> else
> button[n]->is_on();
> }
>
>
>
> void elevator::close_doors()
> {
> cout << "Doors are closing\n";
> cout << "\a\a\a\a\a";
> // If a button is pushed on a floor higher than the current floor,
> then the elevator //always moves up.
> if(button_active(current_floor))
> cout << "Elevator accending\n";
> else
> cout << "Elevator decending\n";
> sleep(1);
> // Keep looping until floor buton is off
> while(buttons.is_off(current_floor))
> {
> if(button_active(current_floor))
> ++current_floor;
> else
> --current_floor;
> if(buttons.is_off(current_floor) && floor_is_valid(current_floor))
> {
> cout << "\tPassing floor " << current_floor << '\a' << '\n';
> sleep(1);
> }
> }
> cout << "\tNow on floor " << current_floor << "\a\a\a" << '\n';
> sleep(1);
> buttons.reset(current_floor); //reset is to put button off
> cout << "Doors are open\n";
> sleep(1);
> }
>
> bool elevator::button_active(int f) const
> {
> for(int i = f; i <= top_floor; ++i)
> if(buttons.is_on(i))
> return true;
> return false;
> }
>
> bool elevator::floor_is_valid(int q) const
> {
> return buttons.is_valid(q);
> }
>
> int main()
> {
> const int top = 9;
> elevator otis(top);
> otis.prompt();
> return 0;
> }
>


You have written too much code without doing any testing. Please pay
attention.


  Réponse avec citation
Vieux 16/10/2007, 02h37   #7
whitehatmiracle@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: The elevator simulation


> You have written too much code without doing any testing. Please pay
> attention.- Hide quoted text -


WOOW, soo many errors!!
Ill go through them in detail and get to you all, thank you!

PS. "include.h" is a header file in which i have include all regular h
files, iostream, string, dos ....

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h26.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20876 seconds with 15 queries