Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;模板和朋友,链接器错误_C++_C++11 - Fatal编程技术网

C++ C++;模板和朋友,链接器错误

C++ C++;模板和朋友,链接器错误,c++,c++11,C++,C++11,为什么不编译以下代码(链接器错误、未解析的外部符号、class std::basic_ostream&\u cdecl运算符此行): friend std::ostream& operator<<(std::ostream& Stream, const Class& Op); friend std::ostream&operator此行: friend std::ostream& operator<<(std::ostream&

为什么不编译以下代码(链接器错误、未解析的外部符号、
class std::basic_ostream&\u cdecl运算符此行):

friend std::ostream& operator<<(std::ostream& Stream, const Class& Op);
friend std::ostream&operator此行:

friend std::ostream& operator<<(std::ostream& Stream, const Class& Op);

friend std::ostream&operator查找friend函数的语义。您还必须将类内的friend函数声明为模板。查找friend函数的语义。您还必须将类内的friend函数声明为模板。谢谢,它可以工作。但是如果我想要接口和imp呢不同文件中的元素?如上所述:模板的定义必须在隐式实例化点可见,这就是模板库通常在标题中提供所有模板定义的原因。@Mario您可以#include“.cpp”`在文件末尾,在头保护之前
#endif
,将模板实现移动到cppfile@Varaquilex包含.cpp文件远不是一个好的做法。有关模板的更多信息,请参见。谢谢,它很有效。但是,如果我想在不同的文件中包含接口和实现,该怎么办?如上所述:模板的定义必须在隐式实例化时可见,这就是模板库通常在标题中提供所有模板定义的原因。@Mario您可以#include“.cpp”`在文件末尾,在头保护之前
#endif,将模板实现移动到cppfile@Varaquilex包含.cpp文件远不是一个好的做法。有关模板的更多信息,请参阅。
template <class T> friend std::ostream& operator<<(std::ostream& Stream, const Class<T>& Op);