Error in class constructor

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2005
Posts: 62
Reputation: freemind is an unknown quantity at this point 
Solved Threads: 1
freemind's Avatar
freemind freemind is offline Offline
Junior Poster in Training

Error in class constructor

 
0
  #1
May 17th, 2006
Hi to all! I'm writing a simple simulation program for elevators but i get a problem, namely:

  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:

  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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,450
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Error in class constructor

 
0
  #2
May 17th, 2006
You don't have a default constructor for Button.

Also:
  1. ButtonSet[i] = new Button(1, 0);
You need a pointer with new.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 501
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 50
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

Re: Error in class constructor

 
0
  #3
May 17th, 2006
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,
  1. stats = Status(1);
this needs to be
  1. stats = idle;
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 62
Reputation: freemind is an unknown quantity at this point 
Solved Threads: 1
freemind's Avatar
freemind freemind is offline Offline
Junior Poster in Training

Re: Error in class constructor

 
0
  #4
May 17th, 2006
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
  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..
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,450
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Error in class constructor

 
0
  #5
May 17th, 2006
Without the default constructor, you can't do this:
  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) {
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 62
Reputation: freemind is an unknown quantity at this point 
Solved Threads: 1
freemind's Avatar
freemind freemind is offline Offline
Junior Poster in Training

Re: Error in class constructor

 
0
  #6
May 17th, 2006
Perhaps the second :-) Thanks!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC