繁体   English   中英

模板 class 无法识别好友运算符

[英]Template class not recognizing friend operator

因此,我尝试在模板 class 中包含两个用于输入和 output 运算符的朋友声明,但每次编译代码时,它似乎都无法识别该运算符。 这是我的 header 文件。

错误:严重性代码描述项目文件行抑制 State 错误 LNK2019 未解析的外部符号“class std::basic_ostream<char,struct std::char_traits > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits > &,class 屏幕<5,5> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Screen@$04$04@@@Z)在 function _main 中引用

这似乎是一个链接错误。

屏幕.h

#include<string>
#include<iostream>
template<std::string::size_type, std::string::size_type>
class Screen;

template<std::string::size_type w, std::string::size_type h>
std::ostream& operator<<(std::ostream&,Screen<w, h>&);


template<std::string::size_type w,std::string::size_type h> 
class Screen{

    friend std::ostream& operator<<(std::ostream&, Screen&);
public:
    Screen()=default;
    Screen(const char str=' '):content(w*h,str){}

    Screen& set(char);
    Screen& set(std::string::size_type,std::string::size_type,char);
    char get() const {return content[cursor];}
    inline char get(std::string::size_type,std::string::size_type)const;

    Screen& move(std::string::size_type,std::string::size_type);
    Screen& display(std::ostream& os) { os<<content;  return *this; }



private:
    //void do_display(std::ostream& os) const { os << content; }
    std::string::size_type cursor=0;
    std::string content;
};
//MEMBER FUNCTIONS
template<std::string::size_type w,std::string::size_type h>
inline Screen<w,h>& Screen<w,h>::set(char c){
    content[cursor] =c;
    return *this;
}

template<std::string::size_type w,std::string::size_type h>
inline Screen<w,h>& Screen<w,h>::set(std::string::size_type r,std::string::size_type col,char ch){
    content[r*w+col]=ch;
    return *this;
}

template<std::string::size_type w,std::string::size_type h>
 inline Screen<w,h>& Screen<w,h>::move(std::string::size_type r,std::string::size_type c){
    cursor = r + w + c;
    return *this;
}

template<std::string::size_type w,std::string::size_type h>
inline char Screen<w,h>::get(std::string::size_type r,std::string::size_type c)const{
    return content[r*w+c];
}

//OS OPERATOR

template<std::string::size_type w, std::string::size_type h>
std::ostream& operator<<(std::ostream& os, Screen<w, h>& item) {
    os << item.content;
    return os;
}

这是我的主要 cpp 文件

主文件

#include"Screen.h"
#include<string>
#include<iostream>

using std::cout; using std::endl;
using std::string;

int main(){
    Screen<5,5> myScreen('X');
    myScreen.move(4, 0).set('#').display(cout);
    cout << endl;
    myScreen.display(cout);
    cout << endl;
    cout << myScreen << endl;
}

我注意到可以通过在 class 正文中包含朋友声明和 function 定义来解决该问题,但我不想这样加点。

public:
... other code

template<std::string::size_type w, std::string::size_type h>
friend std::ostream& operator<<(std::ostream& os, Screen<w, h>& item) {
    os << item.content;
    return os;
}

这让我很困惑,我试图理解为什么它以这种方式工作而不是以其他方式工作,而且我还注意到当我包含这样的朋友声明时

    friend std::ostream& operator<<<w,h>(std::ostream&, Screen&);

它似乎有效,但它给了我一个警告,让我知道没有找到运算符的 function 定义,这让我更加困惑。这个问题来自 c++ 入门第 5 版第 16 章问题 15

friend std::ostream& operator<< <> (std::ostream&, Screen&);
                                --

如果没有这两个字符,friend 声明将声明一个非模板。 也就是说,当您实例化Screen<3,5>时,它将尝试使用此签名查找 function

std::ostream& operator<< (std::ostream&, Screen<3,5>&);

而不是任何 function 模板。 没有这样的 function 所以这会失败。 有效的声明将模板特化声明为友元,并且模板存在并且可以实例化,所以这里没有问题。

friend std::ostream& operator<< <w, h> (std::ostream&, Screen&);

也以完全相同的方式工作。 演示

这些friend元声明要求主模板声明

template<std::string::size_type w, std::string::size_type h>
std::ostream& operator<<(std::ostream&,Screen<w, h>&);

在 scope 中(您的代码就是这种情况)。

class 里面的朋友应该是全专业的:

friend std::ostream& operator<< <>(std::ostream&, Screen&);

注意 function 名称后面的<>

模板啦

暂无
暂无

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

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