0
votes

In this code of operator Overloading, i don't want to write "using namespace std" instead i want to include "std::" wherever required.

After adding "std::" after cout and cin, i am still getting Errors where else to include "std::".

#include<iostream>
//using namespace std;
class Complex
{
private:
  int real, imag;
public:
  Complex(int r = 0, int i = 0) : real(r), imag(i) {}

  friend ostream & operator << (ostream &, const Complex &);
  friend istream & operator >> (istream &, Complex &);
};

ostream & operator << (ostream &out, Complex &obj)
{
  out<<obj.real<<" "<<obj.imag;
  return out;
}
istream & operator >> (istream &in, const Complex &obj)
{
  in>>obj.real>>obj.imag;
  return in;
}

int main()
{
  Complex obj;
  std::cin>>obj;
  std::cout<<obj;
  return 0;
}

It should take input two numbers using istream operator and output two numbers using ostream operator.

3
what errors do you get?463035818_is_not_a_number
what namespace are ostream and istream from?463035818_is_not_a_number

3 Answers

5
votes

add std:: to ostream and istream

They come from the headers <istream> and <ostream> and are defined in <iosfwd>

#include<iostream>
//using namespace std;
class Complex
{
private:
    int real, imag;
public:

    Complex(int r = 0, int i = 0) : real(r), imag(i) {}

    friend std::ostream& operator<<(std::ostream& out, const Complex& obj);
    friend std::istream& operator>>(std::istream& in, Complex& obj);
};

std::ostream& operator<<(std::ostream &out, const Complex &obj)
{
    out << obj.real << " " << obj.imag;
    return out;
}
std::istream& operator>>(std::istream &in, Complex &obj)
{
    in >> obj.real >> obj.imag;
    return in;
}

int main()
{
    Complex obj;
    std::cin >> obj;
    std::cout << obj;
    return 0;
}

(not related to the std:: problem) You can also access your private variables outside the class without the friend declaration by using get/set member functions. Thanks to @aschepler for pointing out my mistake regarding the accessibility.

#include<iostream>
class Complex
{
private:
    int real, imag;
public:
    int get_real() const {
        return real;
    }
    void set_real(int real) {
        this->real = real;
    }
    int get_imag() const {
        return imag;
    }
    void set_imag(int imag) {
        this->imag = imag;
    }

    Complex(int r = 0, int i = 0) : real(r), imag(i) {}

};

std::ostream& operator<<(std::ostream &out, const Complex &obj)
{
    out << obj.get_real() << " " << obj.get_real();
    return out;
}
std::istream& operator>>(std::istream &in, Complex &obj)
{
    int real, imag;
    in >> real >> imag;
    obj.set_real(real);
    obj.set_imag(imag);
    return in;
}

int main()
{
    Complex obj;
    std::cin >> obj;
    std::cout << obj;
    return 0;
}
0
votes

Your favourite standard library reference tells you what namespace things are in. In a tiny program like this, you can simply look up each one in turn.

Hint: they're all in std.

So that includes std::ostream and std::istream.

0
votes

This is just a namespace pollution problem. The importance of it may vary between usages.

When you are just prototyping, using namespace std; is fine, as is including useless headers just in case you need something from one. When you want a super safe code that will be extensively reviewed, you want to prevent name collision and namespace pollutino, so you include in the current namespace only what is required, and you give explicit namepaces for idendifiers that are only seldom used.

The following is more of my own opinion (or more exactly the way I am used to work):

  • when a symbol is seldom used, I give it its explicit namespace (eg: std::cout << i;)
  • when a symbol is heavily used in a compilation unit, I import specifically that symbol (eg: using std::cout; ... cout << i; ... cout << j; ...)