如何重载模板类中的输入和输出运算符? [重复]

我有一个模板类。 我有两种方法-第一种初始化对象的字段,第二种-在屏幕上显示字段。

我需要开发友好的功能“ >>”和“ <<”,它们将执行与现有方法相同的操作。

老实说,对此没有任何想法。有人可以告诉我该怎么做吗?我了解我的问题将不胜感激,但我确实需要帮助=(

充分:

#include <iostream>
using namespace std;

template <typename T1, typename T2, typename T3>
class Parallelepiped {
private:
    T1 edge_1;
    T2 edge_2;
    T3 edge_3;
public:
    Parallelepiped() {
        edge_1 = 0;
        edge_2 = 0;
        edge_3 = 0;
    }
    Parallelepiped(T1 e1, T2 e2, T3 e3) {
        edge_1 = e1;
        edge_2 = e2;
        edge_3 = e3;
    }
    Parallelepiped(const Parallelepiped& obj) {
        this->edge_1 = obj.edge_1;
        this->edge_2 = obj.edge_2;
        this->edge_3 = obj.edge_3;
    }
    void Initialization(void) {
        cout << "\n---------------------------------\n";
        cout << "\n\nEnter edge 1 -->> "; cin >> edge_1;
        cout << "\n\nEnter edge 2 -->> "; cin >> edge_2;
        cout << "\n\nEnter edge 3 -->> "; cin >> edge_3;
        cout << "\n---------------------------------\n";
    }
    void Show(void) {
        cout << "\n---------------------------------\n";
        cout << "\n\nEdge 1 -->> " << edge_1;
        cout << "\n\nEdge 2 -->> " << edge_2;
        cout << "\n\nEdge 3 -->> " << edge_3;
        cout << "\n---------------------------------\n";
    }
    ~Parallelepiped() {
        cout << "Destructor -->> " << this << endl;
    }
};

int main()
{
    setlocale(0, "");
    Parallelepiped<int, int, int> p1;
    Parallelepiped<int, float, float> p2;

    p1.Initialization();
    p2.Initialization();

    p1.Show();
    p2.Show();



    return 0;
}
评论