Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.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 - Fatal编程技术网

C++ 使用模板的输出重载

C++ 使用模板的输出重载,c++,templates,operator-overloading,C++,Templates,Operator Overloading,所以我试图使用模板重载输出操作符,但我一直遇到两个错误。它们是: 错误C2988无法识别的模板声明/定义 及 错误C2143语法错误:中不需要缺少“,”before'” friend ostream& operator<< <E>(ostream& out, const SLinkedList<E>& v); 您还缺少一个然后在 v->next 您在中也使用了相同的模板名称 template <typename E>

所以我试图使用模板重载输出操作符,但我一直遇到两个错误。它们是:

错误C2988无法识别的模板声明/定义

错误C2143语法错误:中不需要缺少“,”before'

friend ostream& operator<< <E>(ostream& out, const SLinkedList<E>& v);
您还缺少一个
然后在

v->next
您在中也使用了相同的模板名称

template <typename E> 
class SLinkedList {
public:

template <typename E>
friend ostream& operator<< <E>(ostream& out, const SLinkedList<E>& v);
};
模板
类链接列表{
公众:
模板
friend ostream&operator试试看

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;
}
模板
类链接列表{
公众:
模板
friend std::ostream和操作员next;
}
返回;
}

您的类缺少一个尾随
。我复制和粘贴时一定错过了它。我已经修好了,对。你的剪贴板还改变了什么?什么是
E const SLinkedLst&v
?从我的书中复制的语法,在重载方面我很迷茫。每当我删除额外的E时,我都会得到一个缺少类型说明符的错误。@Richardrasmusen你真的需要提供一个解决问题的方法。我在SLinkedList中遗漏了一个“I”。
template <typename E> 
class SLinkedList {
public:

template <typename E>
friend ostream& operator<< <E>(ostream& out, const SLinkedList<E>& v);
};
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;
}