"std :: cout<<"中"operator<<"的模棱两可的重载 [英] Ambiguous overload for ‘operator<<’ in ‘std::cout <<

查看:102
本文介绍了"std :: cout<<"中"operator<<"的模棱两可的重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下main.cpp文件

#include "listtemplate.h"
//#include <iostream>
using namespace std;

int main()
{
    int UserChoice;
    cout << "Hello, World!" << endl;
    cin >> UserChoice;
    cout << UserChoice;
}

以当前形式,一切正常.我输入一个整数,该整数被打印到屏幕上.但是,当取消注释cout << "Hello, World!" << endl行时,出现以下错误

In it's current form, everything works. I enter an integer, and that integer is printed to the screen. However, when I uncomment the cout << "Hello, World!" << endl line, I get the following error

main.cpp:10: error: ambiguous overload for ‘operator<<’ in ‘std::cout << "Hello, World!"’

我还可以通过注释掉#include"listtemplate.h",取消注释世界行,并在主行中包含<iostream>(当前可通过模板访问.),有人可以看到我在这里缺少的内容吗?

I can also make it work by commenting out #include "listtemplate.h", uncommenting the hello world line, and including <iostream> in main (currently accessible through the template. Can anyone see what I'm missing here?

listtemplate.h

listtemplate.h

#ifndef LISTTEMPLATE_H
#define LISTTEMPLATE_H
#include "list.h"
using namespace std;

// Default constructor
template <class Type>
list<Type> :: list() : Head(NULL) {}

// Destructor
template <class Type>
list<Type> :: ~list()
{
    Node *Temp;
    while (Head != NULL)
    {
        Temp = Head;
        Head = Head -> Next;
        delete Temp;
    }
}

// Copy constructor
template <class Type>
list<Type> :: list (const Type& OriginalList)
{
    Node *Marker;
    Node *OriginalMarker;

    OriginalMarker = OriginalList.Gead;
    if (OriginalMarker == NULL) Head = NULL;
    else
    {
        Head = new Node (OriginalMarker -> Element, NULL);
        Marker = Head;
        OriginalMarker = OriginalMarker -> Next;

        while (OriginalMarker != NULL)
        {
            Marker -> Next = new Node (OriginalMarker -> Next);
            OriginalMarker = OriginalMarker -> Next;
            Marker = Marker -> Next;
        }
    }
}

// Copy assignment operator
template <class Type>
list<Type>& list<Type> :: operator= (const list<Type>& Original)
{
    Node *Marker;
    Node *OriginalMarker;

    // Check that we are not assigning a variable to itself
    if (this != &Original)
    {
        // First clear the current list, if any
        while (Head != NULL)
        {
            Marker = Head;
            Head = Head -> Next;
            delete Marker;
        }

        // Now build a new copy
        OriginalMarker = Original.Head;
        if (OriginalMarker == NULL) Head = NULL;
        else
        {
            Head = new Node (OriginalMarker -> Element, NULL);
            Marker = Head;
            OriginalMarker = OriginalMarker -> Next;

            while (OriginalMarker != NULL)
            {
                Marker -> Next = new Node (OriginalMarker -> Element, NULL);
                OriginalMarker = OriginalMarker -> Next;
                Marker = Marker -> Next;
            }
        }
    }
    return (*this);
}

// Test for emptiness
template <class Type>
bool list<Type> :: Empty() const
{
    return (Head == NULL) ? true : false;
}

// Insert new element at beginning
template <class Type>
bool list<Type> :: Insert (const Type& NewElement)
{
    Node *NewNode;
    NewNode = new Node;
    NewNode -> Element = NewElement;
    NewNode -> Next = Head;
    return true;
}

// Delete an element
template <class Type>
bool list<Type> :: Delete (const Type& DelElement)
{
    Node *Temp;
    Node *Previous;

    // If list is empty
    if (Empty()) return false;

    // If element to delete is the first one
    else if (Head -> Element == DelElement)
    {
        Temp = Head;
        Head = Head -> Next;
        delete Temp;
        return true;
    }

    // If the list has only one element which isn't the specified element
    else if (Head -> Next == NULL) return false;

    // Else, search the list element by element to find the specified element
    else
    {
        Previous = Head;
        Temp = Head -> Next;

        while ((Temp -> Element != DelElement) && (Temp -> NExt != NULL))
        {
            Previous = Temp;
            Temp = Temp -> Next;
        }

        if (Temp -> Element == DelElement)
        {
            Previous -> Next = Temp -> Next;
            delete Temp;
            return true;
        }
        else return false;
    }
}

// Print the contents of the list
template <class Type>
void list<Type> :: Print (ostream& OutStream) const
{
    Node *Temp;
    Temp = Head;

    while (Temp != NULL)
    {
        OutStream << Temp -> Element << " ";
        Temp = Temp -> Next;
    }
}

// Overloaded output operator
template <class Type>
ostream& operator<< (ostream& OutStream, const list<Type>& OutList)
{
    OutList.Print (OutStream);
    return OutStream;
}
#endif

list.h

#ifndef LIST_H
#define LIST_H
#include <iostream>
#include <cstddef>
using namespace std;

template <class Type>
class list
{
private:
    struct Node
    {
    public:
        Type Element;
        Node *Next;

        Node() : Next(NULL) {} // Default constructor
        Node (Type Data, Node *PNode = NULL) : // Non-default constructor
            Element (Data),
            Next (PNode) {}
    };

    Node *Head;
public:
    list();
    ~list();
    list (const Type& OriginalList);
    bool Empty() const;
    bool Insert (const Type& NewElement);
    bool Delete (const Type& DelElement);
    void Print (ostream& OutStream) const;
    list& operator= (const list<Type>& Original);
};

template <class Type>
ostream& operator<< (ostream& OutStream, const Type& OutList);
#endif

推荐答案

我认为问题在于您的标头中已对该函数进行了原型设计:

I think that the problem is that in your header you've prototyped this function:

template <class Type>
ostream& operator<< (ostream& OutStream, const Type& OutList);

代替这个:

template <class Type>
ostream& operator<< (ostream& OutStream, const list<Type>& OutList);

您原型化的版本表示它是一个operator <<,可以打印任何内容,而不是任何内容的列表.因此,当您编写

The version you've prototyped says that it's an operator << that can print out anything, not lists of anything. Consequently, when you write

cout << "Hello, world!" << endl;

编译器无法确定应该调用哪个函数-标准输出函数还是您在列表标题中定义的函数.

The compiler can't tell which function it's supposed to call - the standard output function or the one you've defined in your list header.

这篇关于"std :: cout&lt;&lt;"中"operator&lt;&lt;"的模棱两可的重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆