ostream 必须只接受一个参数
ostream must take exactly one argument
我的输出流运算符出现编译错误,我似乎无法找到解决方法,因为我以前从未收到过此错误:
linkedList.cpp:258: error: ‘std::ostream& linkedList<T>::operator<<(std::ostream&, linkedList<T>)’ must take exactly one argument
make: *** [linkedList.o] Error 1
这是我的 operator<<
的定义
template <class T>
ostream& linkedList<T>::operator<<(ostream &output, linkedList<T> list)
{
node *curr;
curr=list.head;
while(curr!=NULL )
{
output << curr->data;
curr = curr->next;
}
return output;
}
这是我的 header:
//ostream operator for printing the list
template <class T>
ostream &operator<<(ostream &output, linkedList<T> list);
任何帮助将不胜感激!
你把它定义为一个成员函数,你必须把它定义为一个独立的(可能是朋友)函数,要么
在你的class
之外
template <class U>
ostream& operator<<(ostream &output, linkedList<U> list){...}
在这种情况下,您还必须在 class 中将其声明为
template <class U> // note the different type name
friend ostream& operator<<(ostream &output, linkedList<U> list)
或
里面的class为
// no need for template, type is passed automatically the function is not templated anymore
friend ostream& operator<<(ostream &output, linkedList list){...}
这 2 个声明之间的区别有点微妙,但对于您的目的而言,两者都同样有效。并且可能您想通过 const
引用传递 linkedList
,linkedList<T>& list
.
编辑
一个常见的错误是将 class 中的友元运算符声明为
template<typename T>
class linkedlist
{
//....
friend ostream& operator<<(ostream& output, const linkList& list); // declaration only
}
然后尝试在 class 之外将其定义为
template<typename T>
ostream& operator<<(ostream& output, const linkList<T>& list){...}
你猜怎么着?代码将编译,但不会 link,因为 class 中的声明声明了一个 non-template 函数,每个类型一个 T
你传给 linkList<T>
。然后,当您声明例如linkList<int> lst
,然后尝试 cout << lst
,编译器会看到 friend 的声明,它看起来像
friend ostream& operator<<(ostream& output, const linkList<int>& list);
并将尝试搜索其定义。但是 header 的其余部分没有定义,只有模板运算符,因此 linker 无法找到实现并会吐出 linker 错误。现场示例 here.
都在错误信息里了。您声明的这个函数:
template <class T>
ostream &operator<<(ostream &output, linkedList<T> list);
是 linkedList<T>
的 成员 函数,它接受 两个 个参数。所有二元运算符(+
、*
、<<
、...除调用运算符 ()
之外的所有运算符)在定义为成员函数时,必须只接受一个参数,因此错误。您打算做的是声明一个 非成员 函数,外部 到 class:
template <typename T>
class linkedList { .. };
template <typename T>
ostream &operator<<(ostream &output, linkedList<T> list) {
// implementation
}
您也可以在 class 定义中将其定义为非成员 friend
:
template <class T>
class linkedList {
...
friend ostream &operator<<(ostream &output, linkedList list){
...
}
...
};
我的输出流运算符出现编译错误,我似乎无法找到解决方法,因为我以前从未收到过此错误:
linkedList.cpp:258: error: ‘std::ostream& linkedList<T>::operator<<(std::ostream&, linkedList<T>)’ must take exactly one argument
make: *** [linkedList.o] Error 1
这是我的 operator<<
template <class T>
ostream& linkedList<T>::operator<<(ostream &output, linkedList<T> list)
{
node *curr;
curr=list.head;
while(curr!=NULL )
{
output << curr->data;
curr = curr->next;
}
return output;
}
这是我的 header:
//ostream operator for printing the list
template <class T>
ostream &operator<<(ostream &output, linkedList<T> list);
任何帮助将不胜感激!
你把它定义为一个成员函数,你必须把它定义为一个独立的(可能是朋友)函数,要么
在你的class
之外template <class U> ostream& operator<<(ostream &output, linkedList<U> list){...}
在这种情况下,您还必须在 class 中将其声明为
template <class U> // note the different type name
friend ostream& operator<<(ostream &output, linkedList<U> list)
或
里面的class为
// no need for template, type is passed automatically the function is not templated anymore friend ostream& operator<<(ostream &output, linkedList list){...}
这 2 个声明之间的区别有点微妙,但对于您的目的而言,两者都同样有效。并且可能您想通过 const
引用传递 linkedList
,linkedList<T>& list
.
编辑
一个常见的错误是将 class 中的友元运算符声明为
template<typename T>
class linkedlist
{
//....
friend ostream& operator<<(ostream& output, const linkList& list); // declaration only
}
然后尝试在 class 之外将其定义为
template<typename T>
ostream& operator<<(ostream& output, const linkList<T>& list){...}
你猜怎么着?代码将编译,但不会 link,因为 class 中的声明声明了一个 non-template 函数,每个类型一个 T
你传给 linkList<T>
。然后,当您声明例如linkList<int> lst
,然后尝试 cout << lst
,编译器会看到 friend 的声明,它看起来像
friend ostream& operator<<(ostream& output, const linkList<int>& list);
并将尝试搜索其定义。但是 header 的其余部分没有定义,只有模板运算符,因此 linker 无法找到实现并会吐出 linker 错误。现场示例 here.
都在错误信息里了。您声明的这个函数:
template <class T>
ostream &operator<<(ostream &output, linkedList<T> list);
是 linkedList<T>
的 成员 函数,它接受 两个 个参数。所有二元运算符(+
、*
、<<
、...除调用运算符 ()
之外的所有运算符)在定义为成员函数时,必须只接受一个参数,因此错误。您打算做的是声明一个 非成员 函数,外部 到 class:
template <typename T>
class linkedList { .. };
template <typename T>
ostream &operator<<(ostream &output, linkedList<T> list) {
// implementation
}
您也可以在 class 定义中将其定义为非成员 friend
:
template <class T>
class linkedList {
...
friend ostream &operator<<(ostream &output, linkedList list){
...
}
...
};