简体   繁体   English

尝试重载<<运算符并使用Friend函数时发生错误

[英]error when trying to overload << operator and using friend function

I am trying to overload << operator and using friend function. 我试图重载<<运算符,并使用朋友功能。 Below code chunk works just fine. 下面的代码块工作正常。

template <class T>
class Mystack{
    friend std::ostream& operator<<(std::ostream& s, Mystack<T> const& d)
    {
        d.print(s);
        return s;
    } 
};

Since it is friend function I would obviously want to define it outside the class without using scope resolution operator. 由于它是朋友函数,我显然希望在类之外定义它而不使用范围解析运算符。 But when I try that I get error. 但是当我尝试时,我得到了错误。

template <class T>
class Mystack{
    friend std::ostream& operator<<(std::ostream& s, Mystack<T> const& d); 
};
template <class T>
std::ostream& operator<<(std::ostream& s, Mystack<T> const& d)
{
    d.print(s);
    return s;
}

Below is the code snippet for main 以下是main的代码段

Mystack<int> intstack;
std::cout << intstack;

ERROR : Unresolved extrernal symbol. 错误 :外部符号未解析。

PS: Its not the complete running code. PS:它不是完整的运行代码。 Just a sample. 只是一个样本。 Kindly bear. 请忍受。

friend std::ostream& operator<<(std::ostream& s, Mystack<T> const& d);

declares and befriends a non-template operator<< function. 声明非模板operator<<函数并与之成为朋友。 So Mystack<int> would have as its friend a non-template function std::ostream& operator<<(std::ostream& s, Mystack<int> const& d); 因此Mystack<int>将具有一个非模板函数std::ostream& operator<<(std::ostream& s, Mystack<int> const& d);作为其朋友std::ostream& operator<<(std::ostream& s, Mystack<int> const& d); , etc.

template<class T>
std::ostream& operator<<(std::ostream& s, Mystack<T> const& d)
{
    d.print(s);
    return s;
}

defines an operator<< function template. 定义一个operator<<功能模板。

The two are not the same. 两者不一样。 When you write std::cout << intstack; 当你写std::cout << intstack; , the overload resolution rules resolve it to the non-template operator<< function you declared, but it isn't defined, so you get a linker error. ,重载解析规则会将其解析为您声明的非模板operator<<函数,但未定义该函数,因此会出现链接器错误。

There's no way to define a non-template function for every instantiation of a class template outside the class template. 无法为类模板之外的每个类模板实例化定义非模板函数。 You can, however, befriend a specialization of your operator<< function template: 但是,您可以成为您的operator<<功能模板的专业人士的朋友:

// forward declarations
template <class T>
class Mystack;
template <class T>
std::ostream& operator<<(std::ostream& s, Mystack<T> const& d);

template <class T>
class Mystack
{
    friend std::ostream& operator<< <T>(std::ostream& s, Mystack<T> const& d); 
//                                  ^^^
};

or befriend every specialization of the function template, which is worse from an encapsulation point of view (since, eg, operator<< <int> would be a friend of Mystack<float> ): 或与功能模板的每个专业化做朋友,这从封装的角度来看更糟(因为,例如, operator<< <int>将是Mystack<float>的朋友):

template <class T>
class Mystack
{
public:
    template <class U>
    friend std::ostream& operator<<(std::ostream& s, Mystack<U> const& d);
};

or just define the friend function inside the class. 或者只是在类中定义friend函数。

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

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