如何重载operator <<
。
重载运算符的目的是:
cout << ptr->info
并且没有收到内存地址,但显示该节点信息部分的制造商年份和型号。
示例:
template <class DataType>
struct Node {
DataType info;
Node<DataType> *next;
};
在Node的每个信息部分中都会有一个这样的结构:
struct Car {
string maker;
string year;
string model;
}
到目前为止,我有这个,但它似乎不起作用:
friend ostream &operator << ( ostream &output, Node<DataType> &rlist ) { //Overloaded <<
output << rlist->info.make << endl;
output << rlist->info.year << endl;
output << rlist->info.price << endl;
return output;
}
当我使用g ++编译时,我收到此错误:
LinkedList.cpp: In member function ‘void LinkedList<DataType>::EscribaUltimo() [with DataType = CarType]’:
main.cpp:37: instantiated from here
LinkedList.cpp:15: error: no match for ‘operator<<’ in ‘std::cout << ptr->Node<CarType>::info’
答案 0 :(得分:2)
虽然我有点困惑,因为你实际的主要代码丢失了。我将假设你有一个节点,从遍历链接,现在想要打印它:
#include <iostream>
#include <string>
using namespace std; // not recommended, but useful
// in snippets
// T is usually used, but this is of course up to you
template <class T>
struct Node
{
typedef T value_type; // a usual typedef
value_type info;
Node<value_type> *next;
};
struct Car
{
string maker;
string year;
string model;
}; // you had a missing ;, probably copy-paste error
// this creates a node. normally you'd want this to be
// wrapped into a list class (more on this later)
template <typename T>
Node<T> *createNode(const T& info = T())
{
// allocate node
Node<T> *result = new Node<T>;
result->info = info;
result->next = 0; // no next
return result; // returning a pointer means
// whoever gets this is
// responsible for deleting it!
}
// this is the output function for a node
template <typename T>
std::ostream& operator<<(std::ostream& sink, const Node<T>& node)
{
// note that we cannot assume what node contains!
// rather, stream the info attached to the node
// to the ostream:
sink << node.info;
return sink;
}
// this is the output function for a car
std::ostream& operator<<(std::ostream& sink, const Car& car)
{
// print out car info
sink << "Make: " << car.maker <<
"\nYear: " << car.year <<
"\nModel: " << car.model << std::endl;
return sink;
}
int main(void)
{
// a car list
typedef Node<Car> CarList;
// a couple cars
Car car1 = {"Dodge", "2001", "Stratus"};
Car car2 = {"GMan's Awesome Car Company", "The future", "The best"};
CarList *head = createNode(car1); // create the first node
head->next = createNode(car2);
// now traverse the list
CarList *iter = head;
for (; iter != 0; iter = iter->next)
{
// output, dereference iterator to get the actual node
std::cout << "Car: " << *iter << std::endl;
}
// dont forget to delete!
iter = head;
while (iter)
{
// store next
CarList *next = iter->next;
// delete and move on
delete iter;
iter = next;
}
}
现在,如果您不必创建自己的链接列表,请使用标准链接列表,它可以极大地简化您的任务:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <string>
using namespace std;
struct Car
{
string maker;
string year;
string model;
};
// this is the output function for a car
std::ostream& operator<<(std::ostream& sink, const Car& car)
{
// print out car info
sink << "Make: " << car.maker <<
"\nYear: " << car.year <<
"\nModel: " << car.model << std::endl;
return sink;
}
int main(void)
{
// a car list
typedef std::list<Car> CarList;
// a couple cars
Car car1 = {"Dodge", "2001", "Stratus"};
Car car2 = {"GMan's Awesome Car Company", "The future", "The best"};
CarList cars;
cars.push_back(car1);
cars.push_back(car2);
// now traverse the list (copy to ostream)
std::copy(cars.begin(), cars.end(),
std::ostream_iterator<Car>(std::cout,"\n"));
// delete done automatically in destructor
}
希望这有帮助。
答案 1 :(得分:1)
只是为了确保,你有
template<class DataType>
在运营商定义之前,对吗?如果我这样做,它对我有用。错误消息显示代码中的行号,但粘贴定义中的位置是什么?阅读它,我认为问题不在于
Node<DataType> myNode;
output << myNode
但是
output << myNode.info
没有运算符&lt;&lt;为它定义。
编辑:通过你的评论,听起来你想要定义一个&lt;&lt;汽车的操作员。所以,我会做(未经测试)
ostream& operator<< (ostream& output, Car& car) {
output << car.foo << end;
output << car.bar << end;
return output;
}
template <class DataType>
ostream& operator<< (ostream& output, Node<DataType>& node ) {
output << node.info << end;
return output;
}
基本上,这意味着当你专门化你的Node类型并希望使用&lt;&lt;运算符,你需要确保你专门使用的DataType也有&lt;&lt;运营商定义。
答案 2 :(得分:0)
您需要两个运算符:一个用于Node(或Node*
),另一个用于Car:
ostream &operator << ( ostream &output, Car &car ) {
output << car.maker << endl;
output << car.year << endl;
output << car.model << endl;
return output;
}
template<class DataType>
ostream &operator << ( ostream &output, Node<DataType> *rlist ) {
output << rlist->info;
return output;
}