Hello,

Basically I'm trying to use operator<< to display items from a linked list.

coding in the header file.

template <class DataType>
struct Node {
	DataType info;
	Node<DataType> *next;
};

template <class DataType> class LinkedList;
template <class DataType>
ostream & operator<<(ostream & output, const LinkedList<DataType> & rlist);

template <class DataType>
class LinkedList
public:
friend ostream & operator<< <>(ostream & output, const LinkedList<DataType> & rlist);

then the implementation is

template<class DataType>
ostream & operator<<(ostream& output, const LinkedList<DataType> rlist)
{
   Node<DataType> *ptr; //pointer to traverse the list
   ptr = rlist.start; //set current so that it points to
   //the first node
   while(ptr != NULL) //while more data to print
   {
      output << current->info <<" ";
      ptr = ptr->info; 
   }
    return output;
}

and when I try to display items in the main

#include <iostream>
using namespace std;
struct Account
{
	int accountNumber;
	float accountBalance;
	string customerName;
	string customerAddress;
};

int main()
{
  // 9 is int id, float is 2000, string Name, string City
  Account ac1 = {9, 2000, "myname", "city"};

  LinkedList<Account> accounts;

   accounts.insert(ac1);

cout << accounts;
   
   

return 0;
}

I bet I'm doing something stupid, so please help and explain if I'm doing something wrong.

Thanks.

Are the postings direct copy paste of the code?

If so, then you have two versions of << listed, one as friend and one a routine class method. Both versions in the declaration call for a reference to a list, whereas in the definition you are only using a list, not a reference to a list. These errors would be easy to do retyping code, but would be a problem if code is cut and pasted.

You don't need to loop through the nodes in your overload << operator....Just print the current one and then point to the next node to print.

Yes they are copy and paste directly from the code, I use the friendly function and the one on the top before the class definition because people say that it could resolve the problem.

The error happens when I try to print the list with cout << causes ambiguity error.

The ambiguity comes from having both declarations. Comment out the class method and use the one declared as friend to the class, since you've already written it, but be sure to keep the parameters the same type by making it a reference to a list in the definition. Also, the current pointer is ptr, not current, so output ptr->info and to get to the next node in the list you go to ptr->next, not ptr->info.

Thanks for the help

I got rid of the outer operator<< and working with the friend one only.

I tried to compile the program but now it is giving me a different error:

template<class DataType>
ostream & operator<<(ostream& output, const LinkedList<DataType> & rlist)
{
   Node<DataType> *ptr; //pointer to traverse the list
   ptr = rlist.start; //set current so that it points to

   output << ptr->info << " "; // ************ ERROR **********//
   ptr = ptr->next; 
   return output;
}

this is the description of it.

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Account' (or there is no acceptable conversion)

Accounts doesn't have a << overloaded for it. Therefore, since output << ptr->info is actually trying to display an object of type Account, it can't be displayed by <<, because << doesn't know anything about type Account. If Datatype were something that had a << associated with it, like int or char, etc, then it should work.

hi
i am new one in vast valley of c++..actually i want to make a new library but i know nothing about it..plz help me...
if i want to make "mymath.h" library similar to <math.h> then how i will add mysin mycos and other similar functions into it...plz help me

3alik:
1) Welcome to Daniweb.

2) To be as repectful as possible to every one who posts there are some rules it pays to follow. One of those is to post your request in a new thread if your question has nothing to do with the thread you're tempted to post in.

3) I've never written a "library" so maybe there's something special to it I don't know about. However, you can easily write a group of functions, include them in a header file, and then include that header file in projects you want to use one of the functions in. If you're new enough to C++ not to know how to write functions or create a header file, etc, then it's probably not the time to try writing your own library.

Thanks a lot for the help, I was finally able to finish the code. I knew I was doing something stupid hehe :)


Happy coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.