943,936 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4465
  • C++ RSS
May 17th, 2006
0

Error in class constructor

Expand Post »
Hi to all! I'm writing a simple simulation program for elevators but i get a problem, namely:

C++ Syntax (Toggle Plain Text)
  1.  
  2. elevator.cpp: In constructor `Elevator::Elevator(int)':
  3. elevator.cpp:41: error: no matching function for call to `Button::Button()'
  4. elevator.cpp:10: note: candidates are: Button::Button(const Button&)
  5. elevator.cpp:16: note: Button::Button(int, bool)
  6. elevator.cpp:41: error: no matching function for call to `Button::Button()'
  7. elevator.cpp:10: note: candidates are: Button::Button(const Button&)
  8. elevator.cpp:16: note: Button::Button(int, bool)
  9. elevator.cpp:44: error: `Buttonset' undeclared (first use this function)
  10. elevator.cpp:44: error: (Each undeclared identifier is reported only once for each function it appears in.)
  11. elevator.cpp:45: error: no match for 'operator=' in '((Elevator*)this)->Elevator::Alert = (((Button*)operator new(8u)), (<anonymous>->Button::Button(3, 0), <anonymous>))'
  12. elevator.cpp:10: note: candidates are: Button& Button::operator=(const Button&)

The code that produces this minor irritation is the constructor of the 2nd class:

C++ Syntax (Toggle Plain Text)
  1.  
  2. class Button {
  3. private:
  4. enum Types {inside, outside, alarm};
  5. Types type;
  6. bool active;
  7. public:
  8. Button(int typ, bool state) {
  9. if (typ!=1 && typ!=2 && typ!=3)
  10. throw ("Crappy button type in initialisation...Terminating.");
  11. if(state!=0 && state!=1)
  12. throw("Boolean expected in button class initialisation... Terminating.");
  13. type = Types(typ-1);
  14. active = state;
  15. }
  16.  
  17. inline bool is_active(void) { return active; };
  18. inline int who(void) { return type; };
  19. inline void flashb(void) { active=!active; };
  20. inline void press(void) { active = 1; };
  21. };
  22. class Elevator {
  23. private:
  24. int number;
  25. Button ButtonSet[10];
  26. Button Alert;
  27. queue<int> queue;
  28. int last_stop;
  29. enum Status { moving, idle, alert };
  30. Status stats;
  31. public:
  32. Elevator(int num) {
  33. number = num;
  34. for(int i=0; i<10; i++)
  35. Buttonset[i] = new Button(1, 0);
  36. Alert = new Button(3, 0);
  37. stats = Status(1);
  38. };
  39. void pressb (int num);
  40. void emergency(void);
  41. inline void set_stats(int status) { stats = Status(status-1); } ;
  42. inline int get_stats(void) { return (int)stats; };
  43. int move(void);
  44. };

I would be thankful if somebody shares an idea of the possible error!
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
freemind is offline Offline
62 posts
since Jun 2005
May 17th, 2006
0

Re: Error in class constructor

You don't have a default constructor for Button.

Also:
C++ Syntax (Toggle Plain Text)
  1. ButtonSet[i] = new Button(1, 0);
You need a pointer with new.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 17th, 2006
0

Re: Error in class constructor

You're trying to create objects of type Button, but don't have the constructors to match. Remember that if you provide your own constructor within a class, then you do not get a default constructor.

You also seem to have other problems - you can't assign an int to an object of an enumerated type. eg,
C++ Syntax (Toggle Plain Text)
  1. stats = Status(1);
this needs to be
C++ Syntax (Toggle Plain Text)
  1. stats = idle;
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
May 17th, 2006
0

Re: Error in class constructor

Default constructor i don't need in this case, because i need to set up some predifined values for buttons and states. I'm still a bit confused how to edit my code so it's working, though. As for the
C++ Syntax (Toggle Plain Text)
  1.  
  2. stats = Status(1);
maybe it's an error but i haven't done any C++ programming in quite a while and also the compiler doesn't recognize it as an error. Until the testing comes a lot of code is to be written but the pebble in the shue is still there..
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
freemind is offline Offline
62 posts
since Jun 2005
May 17th, 2006
0

Re: Error in class constructor

Without the default constructor, you can't do this:
C++ Syntax (Toggle Plain Text)
  1. Button ButtonSet[10];
Perhaps you meant this:
Button *ButtonSet[10];
Button *Alert;

---

Or perhaps this for a default constructor?
Button(int typ = 1, bool state = 0) {
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 17th, 2006
0

Re: Error in class constructor

Perhaps the second :-) Thanks!
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
freemind is offline Offline
62 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reccomended Books and Compilers
Next Thread in C++ Forum Timeline: Desperate and eagar to learn!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC