Hi all and thanks in advance for your help !
I have a very simple class called circle and i wanna add the ostream << so i can have the ability in main to write cout<<a<<b<<c;

the theme is i'm getting many compile errors in the same row eg iso c++ forbids the declaration of ostream with no type

here's the code

circle.h



#ifndef CIRCLE_H
 #define CIRCLE_H


class Circle
 {
 private : float radius;
 public:

 Circle();
 Circle(float r);
 float getarea();
 float getperimeter();
 friend ostream &operator(ostream &mystream,Circle &c);
 };

#endif 


circle.cpp


#define PI 3.14159
 #include "circle.h" 

using namespace std;

Circle::Circle()
 {
 radius=0;
 }


Circle::Circle(float r)
 {
 radius=r;
 }

float Circle::getarea()
 {
 return PI*radius*radius;
 }

float Circle::getperimeter()
 {
 return Pi*2*radius;
 }

ostream &Circle::operator(ostream &mystream,Circle &c)
 {
 mystream<<" radius "<<c.radius<<" area "<<c.getarea()<<" perimeter "<<c.getperimeter()<<endl;
 return mystream;
 }

main.cpp

#include <cstdlib>
 #include <iostream>
 #include "circle.h"

using namespace std;

int main(int argc, char *argv[])
 {
 Circle a(10);
 Circle b(5);
 Circle c(4);


 cout<<a<<b<<c;

 system("PAUSE");
 return EXIT_SUCCESS;
 }

In line 18 you haven't defined the operator. Try

friend ostream &operator<<(ostream &mystream, Circle &c);

I don't think you want to have it scoped to the Circle class. If it was then you wouldn't have to be a friend.

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