“未定义引用‘operator>>(std::istream&, Complex&)”对于模板 Complex

发布于 2025-01-15 04:00:10 字数 1450 浏览 2 评论 0原文

我的代码:

#include <iostream>
using std::cin;
using std::cout;
using std::istream;
using std::ostream;

template<typename T>
class Complex
{
    T real, img;
public:
    Complex():real(0), img(0){}
    friend istream& operator>>(istream& input, Complex& c1);
    friend ostream& operator<<(ostream& output, Complex& c1);
    Complex operator+(Complex& c1);
};

template<typename T>
istream& operator>>(istream& input, Complex<T>& c1)
{
    cout<<"Real: ";
    input>>c1.real;
    cout<<"Imag: ";
    input>>c1.img;
    return input;
}

template<typename T>
ostream& operator<<(ostream& output, Complex<T>& c1)
{
    output<<c1.real<<"+"<<c1.img<<"i";
    return output;
}

template<typename T>
Complex<T> Complex<T>::operator+(Complex<T>& c1)
{
    Complex temp;
    temp.real = this->real + c1.real;
    temp.img = this->img + c1.img;
    return temp;
}

int main()
{
    Complex<int> cmp1;
    cin>>cmp1;
    return 0;
}

我收到的错误位于 cin>>cmp1 ,它是 对 'operator>>(std::istream&, Complex&) 的未定义引用'。但我在我的代码中找不到任何问题。

如果我将使用 double 的非模板类复杂化并删除所有与模板相关的代码,则该代码有效,因此 operator>>() 的定义本质上是正确的。

当我将 Complex 设为模板时会发生什么变化?

My code:

#include <iostream>
using std::cin;
using std::cout;
using std::istream;
using std::ostream;

template<typename T>
class Complex
{
    T real, img;
public:
    Complex():real(0), img(0){}
    friend istream& operator>>(istream& input, Complex& c1);
    friend ostream& operator<<(ostream& output, Complex& c1);
    Complex operator+(Complex& c1);
};

template<typename T>
istream& operator>>(istream& input, Complex<T>& c1)
{
    cout<<"Real: ";
    input>>c1.real;
    cout<<"Imag: ";
    input>>c1.img;
    return input;
}

template<typename T>
ostream& operator<<(ostream& output, Complex<T>& c1)
{
    output<<c1.real<<"+"<<c1.img<<"i";
    return output;
}

template<typename T>
Complex<T> Complex<T>::operator+(Complex<T>& c1)
{
    Complex temp;
    temp.real = this->real + c1.real;
    temp.img = this->img + c1.img;
    return temp;
}

int main()
{
    Complex<int> cmp1;
    cin>>cmp1;
    return 0;
}

The error I'm getting is at cin>>cmp1 which is undefined reference to 'operator>>(std::istream&, Complex<int>&)'. But I can't find anything wrong in my code.

The code works if I make complex a non-template class which uses double and remove all template-related code, so the definition of operator>>() is essentially correct.

What changes when I make Complex a template?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开浅夏 2025-01-22 04:00:11

友元函数不是成员,因此它们不是隐式模板。那里的声明表明实例化类型Complex存在非模板运算符。
您可以使用

template<typename U> 
friend istream& operator>>(istream& input, Complex<U>& c1);

template<typename U> 
friend ostream& operator<<(ostream& output, Complex<U>& c1);

Friend functions are not members so they aren't implicitly templates. Declaration there suggests existence of non-template operator for instantiated type Complex<int>.
You may use

template<typename U> 
friend istream& operator>>(istream& input, Complex<U>& c1);

template<typename U> 
friend ostream& operator<<(ostream& output, Complex<U>& c1);
苏佲洛 2025-01-22 04:00:11

问题是目前重载的友元函数都是普通函数。如果您想将它们作为函数模板,那么您需要将类内的友元声明更改为如下所示。

更正式地说,在类模板 Complex<> 的原始代码中 operator<<operator>> 不是函数模板,但“普通”函数在需要时使用类模板实例化。也就是说,它们是模板化实体

有两种方法可以解决该问题,下面给出了这两种方法。

解决方案 1

template<typename T>
class Complex
{
    //other members as before
    
    //friend declarations
    template<typename U>
    friend istream& operator>>(istream& input, Complex<U>& c1);
    template<typename V>
    friend ostream& operator<<(ostream& output, Complex<V>& c1);
    
    //other members as before
};

演示

解决方案 2

//forward declarations
template<typename T>
class Complex;
template<typename U>
istream& operator>>(istream& input, Complex<U>& c1);

template<typename V>
ostream& operator<<(ostream& output,const Complex<V>& c1);

template<typename T>
class Complex
{
    T real, img;
public:
    Complex():real(0), img(0){}
    
    //friend declarations
    
    friend istream& operator>> <T>(istream& input, Complex<T>& c1);
    
    friend ostream& operator<< <T>(ostream& output,const Complex<T>& c1);
    
    Complex operator+(Complex& c1);
};

演示

The problem is that currently the overloaded friend functions are ordinary functions. If you want to make them as function templates instead then you need to change the friend declarations inside the class to as shown below.

To be more formal, in your original code operator<< and operator>> for class template Complex<> are not function templates, but “ordinary” functions instantiated with the class template if needed. That is, they are templated entities.

There are 2 ways to solve the problem, both of which are given below.

Solution 1

template<typename T>
class Complex
{
    //other members as before
    
    //friend declarations
    template<typename U>
    friend istream& operator>>(istream& input, Complex<U>& c1);
    template<typename V>
    friend ostream& operator<<(ostream& output, Complex<V>& c1);
    
    //other members as before
};

Demo

Solution 2

//forward declarations
template<typename T>
class Complex;
template<typename U>
istream& operator>>(istream& input, Complex<U>& c1);

template<typename V>
ostream& operator<<(ostream& output,const Complex<V>& c1);

template<typename T>
class Complex
{
    T real, img;
public:
    Complex():real(0), img(0){}
    
    //friend declarations
    
    friend istream& operator>> <T>(istream& input, Complex<T>& c1);
    
    friend ostream& operator<< <T>(ostream& output,const Complex<T>& c1);
    
    Complex operator+(Complex& c1);
};

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文