所以我试图使用模板重载输出操作符,但是我一直遇到两个错误。他们是:
错误C2988无法识别的模板声明/定义
和
错误C2143语法错误:缺少','之前'<'
template <typename E> class SLinkedList; //forward declaration
template <typename E>
ostream& operator<< (ostream& out, const SLinkedList<E>& v); //forward declaration
template <typename E>
class SLinkedList {
public:
template <typename E>
friend ostream& operator<< <E>(ostream& out, const SLinkedList<E>& v);
};
template <typename E>
ostream& operator <<(ostream& out, E const SLinkedLst<E>& v) {
while (v->next != NULL) {
out << v->elem;
v->next;
}
return out;
}
答案 0 :(得分:1)
<E>
friend ostream& operator<< <E>(ostream& out, const SLinkedList<E>& v);
摆脱它,它应该编译。
在课程结束时,您也错过了;
。在C ++中,class
和struct
声明必须以;
中有额外的
E
ostream& operator <<(ostream& out, E const SLinkedLst<E>& v) {
^ what is this doing here?
您在
结束时也遗漏了;
v->next
您在
中也使用相同的模板名称template <typename E>
class SLinkedList {
public:
template <typename E>
friend ostream& operator<< <E>(ostream& out, const SLinkedList<E>& v);
};
哪个E
也是指的功能?您需要将其更改为其他名称。
答案 1 :(得分:0)
尝试改为
template <typename E>
class SLinkedList {
public:
template <typename T>
friend std::ostream& operator << (std::ostream& out, const SLinkedList<T>& v);
};
template <typename E>
std::ostream& operator << (std::ostream& out, const SLinkedList<E>& v) {
while (v->next != NULL) {
out << v->elem;
v->next;
}
return out;
}