2

i have some problems with template operator<< having a code:

class Manager
{

    multiset<Advertising*, CompareAddPtr > AddToSend;
    LinkedList<Client  > ClientList;
    LinkedList<Client  > ActiveClientList;
    list<string> initList;
    list<string> commandsList;
}

in this class i try to use this method:

void Manager:: PrintAllClientDetialsTofile()
{
    ofstream myfile;
    myfile.open ("Result.txt",ios::app);
    myfile << ClientList;
    myfile << "\n";
    myfile.close();
}

my << function in the template class:

template <class L> ostream & operator<<(ostream& os,const LinkedList<L>& listToprint)
 {
     Link<T> * tmp = listToprint->pm_head;
     for(int i=0;i<listToprint.GetNumOfElements();i++)
     {
         os<<*(tmp->m_data);
         tmp=tmp->m_next;
     }
     return os;
 }

i also have template class

template <class T> class Link {
private:
    T* m_data;
    Link* m_next;
    Link* m_prev;

the client class:

#pragma once
#ifndef _CLIENT_H_
#define _CLIENT_H_
#include <string>
#include <set>
#include "Advertising.h"
#include "Email.h"
#include "FaceBook.h"
#include "Msn.h"
#include "Sms.h"
#include "Bill.h"
#include <ostream>
#include "LinkedList.h"
using namespace std;
struct CompareAddPtrClient : public std::binary_function<Advertising*, Advertising*, bool>
{
    bool operator()(Advertising* x, Advertising* y) const
    {   
        if(x->GetMadeOrderTime()<y->GetMadeOrderTime())
            return true;

        else
            return false;
    }
};

class Client
{
    string m_clientname;
    string m_companyName;
    string m_cod;//check if have length of 15
    string m_telephone;
    string m_email;
    int m_lastOrder;
    int m_orderUntill;

    multiset<Advertising*, CompareAddPtrClient > m_clientsAdds;
    LinkedList<Bill  > BillsForClient;


public:
    Client(string clientName,string companyName,string cod,string telephone,string email);
    Client(string clientName);
    ~Client(void);
    //getters
    LinkedList<Bill  >* GetClientsBills(){return  &BillsForClient;}
    const string GetTelephoneNum()const {return m_telephone;}
    const string GetEmail()const{return m_email;}
    const int GetClientUntill()const{return m_orderUntill;}
    const int GetLastOrderTime()const{return m_lastOrder;}
    //setters
    void UpdateLastTimeMadeOrder(int theday){m_lastOrder=theday;}
    void UpdateOrderUntill(int theday){m_orderUntill=theday+7;}

    //functions
    friend ostream & operator<<(ostream& os,const Client& print);
    bool operator==(const Client & other)const;
    void PrintOrdersForClient()const;
    void AddAdvertuseForClient(Advertising * add);
    void LastOrdersByClient();
    void PrintClientsBills();
};

#endif

when i use this function to print my list i get compile error:

error C2593: 'operator <<' is ambiguous

i found similar question that was asked the question but i don't understand how to solve this problem.

thank you :)

Community
  • 1
  • 1
inna karpasas
  • 601
  • 2
  • 7
  • 6
  • Why are there two different classes for linked lists in that example? – Thanatos Jan 01 '11 at 09:03
  • 3
    Also, your `operator <<` uses `T`, but the template type is `L`. Are you showing us your code, exactly? – Thanatos Jan 01 '11 at 09:06
  • 1
    I really wonder if this would be an extract of real code that will be used by a company, or just an example? – stijn Jan 01 '11 at 10:01
  • Can you post the complete error message? Your compiler should spit a list of available overload candidates. (it may be long, still it's more informative than the code you provided). – Yakov Galka Jan 01 '11 at 10:09
  • this is apart of my universty homework. this is the complete error message i wish the compiler will give me more information. (i am using visoual studio 2010) – inna karpasas Jan 01 '11 at 10:15
  • @inna try GCC if available, G++ prints ambiguous candidates with this error. – 9dan Jan 01 '11 at 16:38

1 Answers1

3

"operator << is ambiguous" is an error that occurs because the compiler has found 2 (or more) << operators that your input types (the output stream and the linked list in this case) apply to. The compiler has two functions, either of which are valid to call, and it doesn't know which it should pick.

Your posted code, however, doesn't show us enough to tell you more than that, and by itself, doesn't seem to have anything that would cause this message. (This error usually comes from multiple pieces of code acting against each other, not from any one spot in the code.) Usually, that error message is followed by the compiler telling you which two operator <

Thanatos
  • 42,585
  • 14
  • 91
  • 146
  • i edit my post. but i dont understand what Confusing the compiler. if the Linke list is of type clients- as i understand the process the compiler should go to templete << operator and whon because the data is from Clint Type go to clients << operator. how can i find the confusing operator for the compiler? – inna karpasas Jan 01 '11 at 09:17