[英]no match for 'operator<<' (with including string and ostream and overloading <<) in c++
I'm trying to build a template for a list with priorities (the template parameters are T for the data and Priority for the priority (for example, if I have a line of students with grades, the T would be the students and their Priority would be their grades). The list contains a Node class, and each Node contains data , priority and a pointer to the next Node. 我正在尝试为具有优先级的列表构建模板(模板参数是数据的T,优先级是优先级(例如,如果我有一排有成绩的学生,则T是学生及其优先级)该列表包含一个Node类,每个Node包含data,priority和指向下一个Node的指针。
I tried to overload the << operator for Node and for the list so i could use <<
for the list (printing each node). 我试图为节点和列表重载<<操作符,这样我就可以将
<<
用于列表(打印每个节点)。 for example: if I want to print the list named receptionHour, using this line: cout << endl << "containing: " << receptionHour << endl;
例如:如果我想打印名为“ receptionHour”的列表,请使用此行:
cout << endl << "containing: " << receptionHour << endl;
The problem is that the complier doesnt recognize the operators I implemented so it doesnt use them and the line won't compile. 问题在于,编译器无法识别我实施的运算符,因此不会使用它们,并且该行也不会编译。 the error i'm getting for each line is:
我得到的每一行的错误是:
no match for 'operator<<' (operand types are 'std::basic_ostream' and 'mtm::PriorityQueue::Node')
'operator <<'不匹配(操作数类型为'std :: basic_ostream'和'mtm :: PriorityQueue :: Node')
here are my implements for the <<
operator, for the list (called PriorityQueue) and for the Node. 这是我用于
<<
操作符,列表(称为PriorityQueue)和Node的实现。 Node(insode the node class witch is inside the list class: Node(表示节点类巫婆在列表类内部:
template<class P, class TT>
friend ostream& operator<<(ostream& os, Node node){
os << "[";
os << node.priority;
os << ",";
os << node.data;
os << "]";
return os;
}
list(called PriorityQueue): 列表(称为PriorityQueue):
template<class P, class TT>
friend ostream& operator<<(PriorityQueue<Priority, T>& queue, std::ostream& os){
Node* nodePtr = queue.head;
Node node;
for(int i = 1; i < queue.sizePQ; i++) {
node = *nodePtr;
os << node;
nodePtr = node.next;
}
return os;
}
thanks! 谢谢!
You are not using the templates nor the operator<<
overload correctly. 您没有正确使用模板,也没有正确使用
operator<<
重载。
(1) When you declare template<class P, class TT>
, your function declaration to use those templates should be function(P first, Class<TT> second)
. (1)声明
template<class P, class TT>
,使用这些模板的函数声明应为function(P first, Class<TT> second)
。 Even though your class definition (and other functions) is something like template<class Name> Class(Name name)
, your cannot use template<class T> function(Class<Name>)
because the template function name must match the template class name. 即使您的类定义(和其他函数)类似于
template<class Name> Class(Name name)
,您也不能使用template<class T> function(Class<Name>)
因为模板函数名称必须与模板类名称匹配。
And the overload for Node
does not need to use template, you know which type your are using ( Node
). 而且
Node
的重载不需要使用模板,您知道您正在使用哪种类型( Node
)。
(2) In the overloading function for operator <<
, the first argument must be the ostream
object. (2)在运算符
<<
的重载函数中,第一个参数必须是ostream
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.