简体   繁体   English

重载友元运算符 >> 用于模板 class

[英]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.只有当我在 Circle 构造函数中为 r 显式赋值时,我才没有收到此错误。 Please make it make sense to me:(请让我觉得有意义:(

The output: 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.根据编译器消息,您似乎正在创建 class 的 object 没有任何参数,因此调用 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.由于您已经实现了 class 的构造函数(即使是参数化构造函数),编译器现在将不会自动添加默认构造函数。

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.我在您的代码中注意到的另一件事是,在重载的友元operator>>中,您通过值而不是通过引用获取了 object,因此您作为输入输入的任何值都是无用的,并且不会保存在预期的目标 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.类似地,对于您重载的朋友operator<< ,您不需要按值将其视为常量 object ,而是按引用常量 object 就可以了,并且效率也更高。 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):所以你需要要么(为了清楚起见,我删除了operator+ ,但请随时将其重新添加):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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