错误 C2593:'运算符 <<'含糊不清

发布于 2024-10-10 08:54:22 字数 3173 浏览 2 评论 0 原文

我对模板运算符有一些问题<< 有一个代码:

class Manager
{

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

在这个类中我尝试使用这个方法:

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

我的<<模板类中的函数:

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;
 }

我还有模板类

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

客户端类:

#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

当我使用此函数打印我的列表时,我收到编译错误:

错误 C2593:“运算符 <<”是 模棱两可

我发现有人问过类似的问题 问题 但我不明白如何解决这个问题。

谢谢 :)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2024-10-17 08:54:22

operator << is ambigacy”是由于编译器发现 2 个(或更多)<< 操作符而发生的错误。您的输入类型(本例中为输出流和链表)适用的运算符。编译器有两个函数,其中任何一个都可以有效调用,但它不知道应该选择哪个。

然而,您发布的代码并没有向我们展示足够多的信息来告诉您更多信息,并且其本身似乎没有任何内容会导致此消息。 (此错误通常来自多段相互影响的代码,而不是来自代码中的任何一个位置。)通常,该错误消息后面跟着编译器,告诉您哪两个运算符 < > 。

"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 <

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文