【问题标题】:error C2593: 'operator <<' is ambiguous错误 C2593:“运算符 <<”不明确
【发布时间】:2011-01-01 08:59:36
【问题描述】:

模板操作符有一些问题

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:'操作员

我发现了类似的问题 the question 但我不明白如何解决这个问题。

谢谢你:)

【问题讨论】:

  • 为什么在那个例子中链表有两个不同的类?
  • 另外,您的operator &lt;&lt; 使用T,但模板类型为L。您是在向我们展示您的代码吗?
  • 我真的很想知道这是否是公司将使用的真实代码的摘录,还是只是一个示例?
  • 你能发布完整的错误信息吗?你的编译器应该给出一个可用的重载候选列表。 (它可能很长,但它仍然比您提供的代码提供更多信息)。
  • 这是我大学作业的一部分。这是完整的错误消息,我希望编译器能给我更多信息。 (我使用的是视觉工作室 2010)

标签: c++ templates operators


【解决方案1】:

"operator &lt;&lt; is ambiguous" 是由于编译器发现您的输入类型(在这种情况下为输出流和链表)适用的 2 个(或更多)

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

【讨论】:

  • 我编辑我的帖子。但我不明白什么混淆了编译器。如果 Linke 列表属于客户端类型——据我了解,编译器应该转到模板
猜你喜欢
  • 2010-11-03
  • 2011-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多