Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++_Templates_Operator Overloading_Friend - Fatal编程技术网

C++ 友元模板重载运算符<&书信电报;:未解析外部符号

C++ 友元模板重载运算符<&书信电报;:未解析外部符号,c++,templates,operator-overloading,friend,C++,Templates,Operator Overloading,Friend,我对这个错误有意见 错误LNK2019未解析的外部符号“class std::basic_ostream>&u cdecl cop4530::operator您还应该在rob名称空间内声明函数签名,它实际上属于哪个名称空间: namespace rob { template <typename T> class Stack { friend std::ostream& operator<< (std::ostream& os, const Stac

我对这个错误有意见


错误LNK2019未解析的外部符号“class std::basic_ostream>&u cdecl cop4530::operator您还应该在rob名称空间内声明函数签名,它实际上属于哪个名称空间:

namespace rob {
template <typename T>
class Stack {
    friend std::ostream& operator<< (std::ostream& os, const Stack<T>& a);
};

template < typename T>
std::ostream& operator<< (std::ostream& os, const Stack<T>& a){
    ...
}
名称空间{
模板
类堆栈{
friend std::ostream和操作员

std::ostream&operator除了@LibertyPaul的答案之外,您还需要在friend
std::os…
行中添加一个
模板
,以便工作:

namespace rob {

   template <typename T> class Stack {
        friend std::ostream& operator<< (std::ostream& os, const Stack<T>& a) {
              return a.arr.print(os, ' ');
        }
   };

}
名称空间{
模板类堆栈{

friend std::ostream&operator代码示例的问题

template <typename T>
class Stack {
    friend std::ostream& operator<< (std::ostream& os, const Stack<T>& a);
    void print(std::ostream& os, char ofc = ' ') const;
    // ...
};
由于它没有实现,链接器无法找到它并导致您得到的错误

作为旁注,gcc警告如下


警告:friend declaration'std::ostream&Operator未解决此问题,但无论如何,它们应该位于不同的文件中(.h用于声明,hpp用于定义)。@Robert Hook函数头和函数实现仍然位于不同的文件中。“friend”"签名只是此函数访问私有或受保护成员/方法的权限。通过此访问,应该存在一个函数本身,该函数本身可以定义为配对:头文件中的签名和cpp文件中的实现。@Robert顺便说一句,内联和模板都不能在类外工作。您应该在头文件中放置此类函数。I g使用模板friend std::ostream&Operator时会遇到链接器错误,但现在出现C2248错误,表示函数无法访问私有成员数据。请使用@LibertyPaul的答案。@RobertHook是导致此问题的模板。没有模板,它可以访问堆栈的私有成员这是使其正常工作的唯一方法k是在类中编写ostream运算符的定义。我编辑了我的答案,让它与模板friend std::ostream&operator一起工作。可能使用
运算符的任何文件都不包含hpp文件。如果您的问题被涵盖,请检查这些文件是否正确地包含在一起,正如t中声明的其他函数一样h文件,并在.hpp文件中定义。只有运算符重载存在链接器问题,我确信如果我能解决其中一个问题,其他问题也会以相同的方式得到解决。我知道您已经解决了此问题,但请首先查看我对错误原因的回答以及可能的替代方案。我找不到合适的dupli这类代码(特别是与未解析的符号相关的代码),所以要求使用+1。对我来说,最晦涩的代码,尤其是“运算符”运算符
namespace rob {

   template <typename T> class Stack {
        friend std::ostream& operator<< (std::ostream& os, const Stack<T>& a) {
              return a.arr.print(os, ' ');
        }
   };

}
template <typename T>
class Stack {
    friend std::ostream& operator<< (std::ostream& os, const Stack<T>& a);
    void print(std::ostream& os, char ofc = ' ') const;
    // ...
};
std::ostream& operator<< (std::ostream& os, const Stack<int>& a) {/*...*/}
// forward declare the Stack
template <typename>
class Stack;

// forward declare the operator <<
template <typename T>
std::ostream& operator<<(std::ostream&, const Stack<T>&);

template <typename T>
class Stack {
    friend std::ostream& operator<< <>(std::ostream& os, const Stack<T>& a);
    // note the required <>        ^^^^
    void print(std::ostream& os, char ofc = ' ') const;
    // ...
};

template <typename T>
std::ostream& operator<<(std::ostream&, const Stack<T>&)
{
  // ... implement the operator
}
template <typename T>
class Stack {
    template <typename T1>
    friend std::ostream& operator<<(std::ostream& os, const Stack<T1>& a);
    // ...
};