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_Friend - Fatal编程技术网

C++ 友人接线员<&书信电报;在模板类中

C++ 友人接线员<&书信电报;在模板类中,c++,templates,friend,C++,Templates,Friend,根据我对friend函数的了解,这应该是可行的。我不知道发生了什么事 在我的代码中,我定义了一个类 template < class IType = unsigned int > class BitArray { ... friend ostream& operator<<(ostream&, const BitArray&); friend istream& operator>>(istream&

根据我对friend函数的了解,这应该是可行的。我不知道发生了什么事

在我的代码中,我定义了一个类

template < class IType = unsigned int >
class BitArray {
    ...
    friend ostream& operator<<(ostream&, const BitArray&);
    friend istream& operator>>(istream&, BitArray&);
    ...
}
当我试图编译时

我已经重读和重写了六遍,并且回顾了“friend”关键字的用法,但是没有发现什么地方错了

由于模板的原因,此实现是否遵循不同的规则


我还将>作为移位运算符重写,但这也不重要,因为它们有不同的参数

有了警告,您就有:

warning: friend declaration 'std::ostream& operator<<(std::ostream&, const BitArray<IType>&)' declares a non-template function [-Wnon-template-friend]
note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
warning:friend declaration'std::ostream&operator我想你想要这个:

template < class IType = unsigned int >
class BitArray {
    template<class> friend ostream& operator<<(ostream&, const BitArray&);
    template<class> friend istream& operator>>(istream&, BitArray&);
};
template
类位数组{
模板友元ostream&运算符(istream&,BitArray&);
};

您的位数组声明告诉编译器查找非模板运算符>

您发布的代码看起来正常。请提供一个答案。你确定吗?模板中的函数将BitArray声明为第二个参数。注释被删除。所以我需要在实现之前在两个不同的位置给出一个原型两次?这似乎很奇怪,为什么?另外,操作中的参数是什么
是一个空模板参数列表。使用我的解决方案可以避免两次声明原型。
warning: friend declaration 'std::ostream& operator<<(std::ostream&, const BitArray<IType>&)' declares a non-template function [-Wnon-template-friend]
note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
template <class IType> class BitArray;
template <class IType> std::ostream& operator<<(std::ostream&, const BitArray<IType>&);
template <class IType> std::istream& operator>>(std::istream&, BitArray<IType>&);

template < class IType = unsigned int >
class BitArray {
    friend std::ostream& operator<< <>(std::ostream&, const BitArray&);
    friend std::istream& operator>> <>(std::istream&, BitArray&);
};

template <class IType>
std::ostream& operator<<(std::ostream& os, const BitArray<IType>& b)
{
    /* Your implementation */
}
template < class IType = unsigned int >
class BitArray {
    template<class> friend ostream& operator<<(ostream&, const BitArray&);
    template<class> friend istream& operator>>(istream&, BitArray&);
};