class and overloading operators

Hello guys,
i am new here and just have begun to programe a little bit so there is something i dont understand i hope you guys can help me with it:
its abot a class i have written so here ist the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #ifndef SMOOTHIE_H
#define SMOOTHIE_H

#include <string>
#include <stdexcept>
#include <vector>
#include <iostream>

using namespace std;

class Smoothie
{
	string bezeichnung;
	vector <Zutat>zutat;
	public:
	Smoothie(string);
	Smoothie(void);
	void hinzu(const Zutat& z);
	int brennwert() const;
	
	ostream& print(ostream&) const;
};
ostream operator<<(ostream&, char Smoothie,& s);

#endif 

so thisis the code and if i compile these with the others i have written theree ist always this operator<< must have two arguments which i dont understand.
The problem is the char before Smoothie and the comma after on line 23:

ostream operator<<(ostream&, Smoothie& s); // Note: , starts a new parameter

You may use const:

ostream operator<<(ostream&, const Smoothie& s);

One way to do it is declare
friend ostream& operator<<(ostream& oss, const Smoothie& s); inside the class either private or public

and define it outside

1
2
3
4
5
ostream & operator<<(ostream& oss, const Smoothie & s)
{
  // TODO output your Smoothie to oss
  return oss;
}
One way to do it is declare
friend ostream& operator<<(ostream& oss, const Smoothie& s); inside the class either private or public


Remember that a friend function doesn't really have a access specifier it can be placed anywhere inside the class.

Also in this case there is really no need to make the overload a member function since there is a public function that can be called (print()).


Topic archived. No new replies allowed.