简体   繁体   中英

overloading friend operator >> for template class

How do I get the user to assign the value to radius using the overloaded friend operator>>? I have no idea why I am getting this error. Only when I explicitly assign a value to r in the Circle constructor is when I do not get this error. Please make it make sense to me:(

The output:

在此处输入图像描述

The code:

#include <iostream>

template <class T>
 class Circle {
    private:
        T radius;
        double area;
    public: 
        Circle(T r){    //constructor
            radius = r;
            area =  3.14 * (radius*radius);
        }

        friend std::istream& operator>> (std::istream & input, Circle<T> circle1){ //friend function 
            input >> circle1.radius;
            return input;
        }
        friend std::ostream& operator<< (std::ostream & output, const Circle<T> circle1){ //friend function 
          output << "\nRadius: "<< circle1.radius << "\nArea: "<< circle1.area;
          return output;
        }
        void  operator+ (T n) { //member function
          radius += n;
        }     
};


int main()
{
    Circle<int> circle2;
    std::cin >> circle2;
    std::cout << circle2;

    return 0;
}

Going by the compiler message, you seem to be creating an object of the class without any parameters, hence calling the default constructor of the class. As you have implemented a constructor (even though a parametrized constructor) of the class, the compiler will now, not automatically add the default constructor.

Also another thing I noticed in your code, is that in the overloaded friend operator>> , you have taken the object by value and not by reference, so any value you enter as input is useless and is not saved in the intended target object.

Similarly for your overloaded friend operator<< , you don't need to take it as a constant object by value, instead a constant object by reference would do just fine and be much more efficient as well. Finally, you should also consider recalculating the area of the circle after taking a new radius value as input from the user.

So you need to either (I have removed operator+ for clarity, but feel free to add it back in):

  1. Ask the compiler to define the default constructor:
#include <iostream>

template <class T>
 class Circle {
    private:
        T radius;
        double area;
        void update_radius() {
            area = 3.14 * (radius * radius);
        }
    public: 
        Circle() = default;
        Circle(T r){    //constructor
            radius = r;
            update_radius();
        }


        friend std::istream& operator>> (std::istream & input, Circle<T> &circle1){ //friend function 
            input >> circle1.radius;
            circle1.update_radius();
            return input;
        }
        friend std::ostream& operator<< (std::ostream & output, const Circle<T> &circle1){ //friend function 
          output << "\nRadius: "<< circle1.radius << "\nArea: "<< circle1.area;
          return output;
        }
};


int main()
{
    Circle<int> circle2;
    std::cin >> circle2;
    std::cout << circle2;

    return 0;
}
  1. Give a default value to your single argument constructor
#include <iostream>

template <class T>
 class Circle {
    private:
        T radius;
        double area;
        void update_radius() {
            area = 3.14 * (radius * radius);
        }
    public: 
        Circle(T r = T{}){    //constructor
            radius = r;
            update_radius();
        }


        friend std::istream& operator>> (std::istream & input, Circle<T> &circle1){ //friend function 
            input >> circle1.radius;
            circle1.update_radius();
            return input;
        }
        friend std::ostream& operator<< (std::ostream & output, const Circle<T> &circle1){ //friend function 
          output << "\nRadius: "<< circle1.radius << "\nArea: "<< circle1.area;
          return output;
        }
};


int main()
{
    Circle<int> circle2;
    std::cin >> circle2;
    std::cout << circle2;

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM