I have this error but cant seem to see why. This code compiled would give 1 error. Anyone please advise, thanks.

#include <string>
using namespace std;
class Beverage{
protected:
 Beverage *beverage;
 string desc;
public: 
 friend class Bev_Iterator;
 Beverage(){
  beverage = 0;
  desc = "unknown beverage";
 }
 virtual string getdesc(){
  return desc;
 }
 virtual double cost(){
  return 0;
 }
 Bev_Iterator* Iterator();
};
Bev_Iterator* Beverage::Iterator(){
 return new Bev_Iterator((Beverage*) this); //error here
};
class Bev_Iterator{
 Beverage *bev, *next;
public:
 Bev_Iterator(Beverage *bev){
  this->bev=bev;
 }
 
 void moveFirst(){
  next=bev;
 }
 Beverage *Next(){
  Beverage *t=next;
  next=next->Beverage;
  return t;
 }
 bool hasNext(){
  return (next!=0);
 }
};

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.

#include <string>
using namespace std;

class Beverage;

class Bev_Iterator
{
protected:
	const Beverage *bev;
	const Beverage *next;
public:
	Bev_Iterator(const Beverage *bev)
	{
		this->bev=bev;
		this->next = 0;
	}
};

class Beverage
{
protected:
	Beverage *beverage;
	string desc;
public: 
	friend class Bev_Iterator;
	Beverage()
	{
		beverage = 0;
		desc = "unknown beverage";
	}
	virtual string getdesc()
	{
		return desc;
	}
	virtual double cost()
	{
		return 0;
	}
	Bev_Iterator* Iterator() 
	{
		return new Bev_Iterator(this); //error here
	}
};

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:

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 ;)

This article has been dead for over six months. Start a new discussion instead.