对于班级,我试图超载< <操作员,以便我可以打印出我创建的对象。我声明,并加入到这一如何重载operator <<与链表?
WORD you; //this is a linked list that contains 'y' 'o' 'u'
,我想这样做
cout << you; //error: no operator "<<" matches theses operands
我要插入运算符重载为友元函数与链接打印字。
我已经声明并定义了重载函数,但它仍然不起作用。下面是类的声明文件,然后用功能
#include <iostream>
using namespace std;
#pragma once
class alpha_numeric //node
{
public:
char symbol; //data in node
alpha_numeric *next;//points to next node
};
class WORD
{
public:
WORD(); //front of list initially set to Null
//WORD(const WORD& other);
bool IsEmpty(); //done
int Length();
void Add(char); //done
void Print(); //dont
//void Insert(WORD bword, int position);
//WORD operator=(const string& other);
friend ostream & operator<<(ostream & out, alpha_numeric *front);//******************<-----------------
private:
alpha_numeric *front; //points to the front node of a list
int length;
};
在.cpp文件中.cpp文件,我把*front
的参数,因为它说没有定义front
当我试图用它里面的函数,尽管我在课堂上声明了它。然后我尝试了这个。如果它是正确的,我不知道。
ostream & operator<<(ostream & out, alpha_numeric *front)
{
alpha_numeric *p;
for(p = front; p != 0; p = p -> next)
{
out << p -> symbol << endl;
}
}
为什么不创建'ostream&operator <<(ostream&out,WORD&word)'? –
@SevaTitov它必须作为一个朋友功能来实现,它在作业方向 – Mike