Member Avatar for Member #953548

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

Dani AI

Generated

The compilation failures stem from a few distinct problems in the posted snippets: the operator token was written incorrectly, the header used ostream without putting it in scope or including a declaration, the operator was implemented as a member (so cout << a cannot find it), and there is a case-sensitive typo (Pi vs PI). intended cout << a << b << c;, and the suggestions from , and point in the right directions — declare a proper non-member operator<<, make public accessors const-correct, and avoid putting using namespace std in a header.

A compact, robust approach: give Circle const-correct accessors (or a toString()), declare the non-member operator<< in the header so overload resolution sees it, and implement the operator in the .cpp using the public API (no friend needed). Prefer constexpr for PI instead of a macro.

Example layout (header + implementation):

/* circle.h */
#ifndef CIRCLE_H
#define CIRCLE_H

#include <string>
#include <iosfwd>

class Circle {
    float radius_;
public:
    explicit Circle(float r = 0.0f);
    float area() const;
    float perimeter() const;
    std::string toString() const;
};

std::ostream& operator<<(std::ostream& os, const Circle& c);

#endif
/* circle.cpp */
#include "circle.h"
#include <sstream>
#include <iomanip>

static constexpr float PI = 3.14159f;

Circle::Circle(float r) : radius_(r) {}
float Circle::area() const { return PI * radius_ * radius_; }
float Circle::perimeter() const { return 2.0f * PI * radius_; }
std::string Circle::toString() const {
    std::ostringstream oss;
    oss << "radius " << radius_ << " area " << area() << " perimeter " << perimeter();
    return oss.str();
}

std::ostream& operator<<(std::ostream& os, const Circle& c) {
    return os << c.toString();
}

Quick checklist: (1) replace the mistaken operator( token with operator<<, (2) declare the operator in the header, (3) make getters const, (4) fix PiPI (or use constexpr), and (5) avoid using namespace std in headers. Applying those changes will make cout << a << b << c; resolve and compile cleanly.

Recommended Answers

All 3 Replies

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

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

Line 53 as well..

ostream &Circle::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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.