Error declaring class, pls help

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2007
Posts: 4
Reputation: kevinmscs is an unknown quantity at this point 
Solved Threads: 0
kevinmscs kevinmscs is offline Offline
Newbie Poster

Error declaring class, pls help

 
0
  #1
Apr 3rd, 2007
I have this error but cant seem to see why. This code compiled would give 1 error. Anyone please advise, thanks.

  1.  
  2. #include <string>
  3. using namespace std;
  4. class Beverage{
  5. protected:
  6. Beverage *beverage;
  7. string desc;
  8. public:
  9. friend class Bev_Iterator;
  10. Beverage(){
  11. beverage = 0;
  12. desc = "unknown beverage";
  13. }
  14. virtual string getdesc(){
  15. return desc;
  16. }
  17. virtual double cost(){
  18. return 0;
  19. }
  20. Bev_Iterator* Iterator();
  21. };
  22. Bev_Iterator* Beverage::Iterator(){
  23. return new Bev_Iterator((Beverage*) this); //error here
  24. };
  25. class Bev_Iterator{
  26. Beverage *bev, *next;
  27. public:
  28. Bev_Iterator(Beverage *bev){
  29. this->bev=bev;
  30. }
  31.  
  32. void moveFirst(){
  33. next=bev;
  34. }
  35. Beverage *Next(){
  36. Beverage *t=next;
  37. next=next->Beverage;
  38. return t;
  39. }
  40. bool hasNext(){
  41. return (next!=0);
  42. }
  43. };
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Error declaring class, pls help

 
0
  #2
Apr 3rd, 2007
Finally got it to compile by removing the code you have after the class declarations and reformatting to make it easier to read. Don't be afraid of using spaces in your program -- crunching everything up like you have it makes debugging difficult if not sometimes impossible.

  1. #include <string>
  2. using namespace std;
  3.  
  4. class Beverage;
  5.  
  6. class Bev_Iterator
  7. {
  8. protected:
  9. const Beverage *bev;
  10. const Beverage *next;
  11. public:
  12. Bev_Iterator(const Beverage *bev)
  13. {
  14. this->bev=bev;
  15. this->next = 0;
  16. }
  17. };
  18.  
  19. class Beverage
  20. {
  21. protected:
  22. Beverage *beverage;
  23. string desc;
  24. public:
  25. friend class Bev_Iterator;
  26. Beverage()
  27. {
  28. beverage = 0;
  29. desc = "unknown beverage";
  30. }
  31. virtual string getdesc()
  32. {
  33. return desc;
  34. }
  35. virtual double cost()
  36. {
  37. return 0;
  38. }
  39. Bev_Iterator* Iterator()
  40. {
  41. return new Bev_Iterator(this); //error here
  42. }
  43. };
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Error declaring class, pls help

 
0
  #3
Apr 3rd, 2007
When you get an error with your code, please post that next time. Anyways, the error I got was that on line 19 you declare a Bev_Iterator* without having declared the class first. Put this line before the definition of the Beverage class:
  1. class Bev_Iterator;
There maybe be other errors, I didn't check thoroughly.

[edit:] look like Ancient Dragon put a li'l more time into it
Last edited by Infarction; Apr 3rd, 2007 at 9:58 pm.
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC