C ++ No<<发现哪个操作员采用右手操作数

时间:2014-04-20 20:44:08

标签: c++ linked-list operator-overloading operands

我正在编写一些C ++作业并遇到麻烦,我无法在我的linkedlist类中运行我的displayList()函数。我收到以下错误。

错误1错误C2679:二进制'&lt;&lt;' :找不到带有'weatherstats'类型右手操作数的操作符(或者没有可接受的转换)c:\ users \ larry \ documents \ visual studio 2013 \ projects \ weatherstats \ weatherstats \ linkedlist.h 100 1 WeatherStats < / em>的

所以我需要重载&lt;&lt;操作数,但我尝试使用我在网上找到的一些例子,但没有运气。有人可以用我的&lt;&lt;来帮助我过载运营商好吗?

编辑:添加了Weatherstats.cpp

编辑2:我创建了一个重载的&lt;&lt;我的weatherstats.cpp文件中的操作数,我在下面更新了我的内容。它不是输出我的数据,而是输入我输入的所有数据1.我输入2表示雪,当使用我的displayList()函数时输出1。

linkedlist.h

#pragma once

#include"WeatherStats.h"
#include<iostream>
#include<string>

using namespace std;


template <class T>
class linkedlist
{
private:

    struct ListNode
    {
        T value;                //Current value of node
        struct ListNode *next;  //Pointer to next node
    };

    ListNode *head;             //Head pointer

public:
    /*!
    Constructors
    !*/
    linkedlist()
    {
        head = NULL;
    };


    /*!
    Destructors
    !*/
    ~linkedlist();

    /*!
    Prototypes
    !*/
    void appendNode(T);         //Append a node to list
    void insertNode(T);         //Insert a node to list
    void deleteNode(T);         //delete a node in list
    void searchList(T);         //Search a node in list
    void displayList() const;           //Display the full list

    friend ostream &operator << (ostream&, linkedlist<T> &);
};


//**
//Append Node
//**
template <class T>
void linkedlist<T>::appendNode(T newValue)
{
    ListNode *newNode;          //Point to new node
    ListNode *nodePtr;          //Move through list

    //Assign newValue to new node
    newNode = new ListNode;
    newNode->value = newValue;
    newNode->next = NULL;

    if (!head)
    {   //If empty assign new node as head
        head = newNode;
    }
    else
    {   //Assign head to nodePtr
        nodePtr = head;

        //Find last node in list
        while (nodePtr->next)
        {
            nodePtr = nodePtr->next;
        }

        //Insert newNode as the last node
        nodePtr->next = newNode;
    }
}

//**
//Display List
//**

template <class T>
void linkedlist<T>::displayList()const
{

    ListNode *nodePtr;

    //Assign head to nodePtr
    nodePtr = head;

    //While nodePtr pointing to a node, print to screen
    while (nodePtr)
    {
        //Print value
        cout << nodePtr->value << endl; //ERROR C2679 HERE

        //Move nodePtr to next node
        nodePtr = nodePtr->next;
    }

}


//**
//Insert Node
//**
template <class T>
void linkedlist<T>::insertNode(T newValue)
{
    ListNode *newNode;
    ListNode *nodePtr;
    ListNode *previousNode = NULL;

    //New node
    newNode = new ListNode;
    newNode->value = newValue;

    //If list is empty assign newValue to head
    if (!head)
    {
        head = newNode;
        newNode->next = NULL;
    }
    else
    {
        //Assign head to nodePtr
        nodePtr = head;

        //Pass over all nodes who are less than newValue
        while (nodePtr != NULL && nodePtr->value < newValue)
        {
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }

        //If new node will be first, insert before all other nodes
        if (previousNode == NULL)
        {
            head = newNode;
            newNode->next = nodePtr;
        }
        else
        {
            previousNode->next = newNode;
            newNode->next = nodePtr;
        }
    }
}


//**
//Delete Node
//**
template <class T>
void linkedlist<T>::deleteNode(T searchValue)
{

    ListNode *nodePtr;              //Traverse our list
    ListNode *previousNode = NULL;  //Points to previous node

    //Check if list is empty
    if (!head)
    {
        cout << "This list is empty." << endl;
        return;
    }

    //Delete head if == searchValue
    if (head->value == searchValue)
    {
        nodePtr = head->next;
        cout << head->value << " deleted" << endl;
        delete head;
        head = nodePtr;
    }

    else
    {
        //Set nodePtr to head
        nodePtr = head;

        //Skip nodes not equal num
        while (nodePtr != NULL && nodePtr->value != searchValue)
        {
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }

        //Link previous node to the node after nodePtr and then delete
        if (nodePtr)
        {
            previousNode->next = nodePtr->next;
            cout << nodePtr->value << " deleted" << endl;
            delete nodePtr;
        }
    }
}


//**
//Search List
//**
template <class T>
void linkedlist<T>::searchList(T searchValue)
{
    ListNode *nodePtr;              //Traverse our list
    ListNode *previousNode = NULL;  //Points to previous node
    int counter = 0;

    //Check if list is empty
    if (!head)
    {
        cout << "This list is empty." << endl;
        return;
    }

    //Check if head == searchValue
    if (head->value == searchValue)
    {
        cout << head->value << " found at position 0" << endl;
    }

    else
    {

        //set nodePtr to head
        nodePtr = head;

        //Pass over all nodes that do not equal searchValue
        while (nodePtr != NULL && nodePtr->value != searchValue)
        {
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
            counter++;
        }

        //When nodePtr == searchValue
        if (nodePtr)
        {
            cout << nodePtr->value << " found at position " << counter << endl;
        }

        else
        {
            cout << "-1: Value not found." << endl;
        }
    }
}


//**
//Destructor
//**
template <class T>
linkedlist<T>::~linkedlist()
{
    ListNode *nodePtr;   // To traverse the list
    ListNode *nextNode;  // To point to the next node

    // Position nodePtr at the head of the list.
    nodePtr = head;

    // While nodePtr is not at the end of the list...
    while (nodePtr != NULL)
    {
        // Save a pointer to the next node.
        nextNode = nodePtr->next;

        // Delete the current node.
        delete nodePtr;

        // Position nodePtr at the next node.
        nodePtr = nextNode;
    }
}




template <class T>
ostream &operator << (ostream stream, linkedlist<T> &obj)
{
    stream >> obj.value;

    return stream;
}

的main.cpp

#include "linkedlist.h"
#include "WeatherStats.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int int_numMonths;      //Hold number of months value
    double dbl_rain;        //Hold rain value
    double dbl_snow;        //Hold snow value
    double dbl_sunnyDays;   //Hold sunny days value

    //Create lnk_list object with weatherstats
    linkedlist<weatherstats>weather_list;

    cout << "Weather Data" << endl;
    cout << endl;
    cout << "What is the number of months you want to enter data for?: ";
    cin >> int_numMonths;
    cout << endl;

    //Loop to enter each months values
    for (int i = 0; i < int_numMonths; i++)
    {
        cout << "Month " << i + 1 << endl;
        cout << "Enter amount of rain: " << endl;
        cin >> dbl_rain;
        cout << "Enter amount of snow: " << endl;
        cin >> dbl_snow;
        cout << "Enter number of Sunny Days: " << endl;
        cin >> dbl_sunnyDays;

        //Create weatherstats obj and pass it rain,snow and sunnyDays
        weatherstats month_data(dbl_rain,dbl_snow,dbl_sunnyDays);

        weather_list.appendNode(month_data);

    }

    weather_list.displayList();

}

Weatherstats.cpp

    #include "WeatherStats.h"

    #include <iostream>

    using namespace std;

    /*!
     Constructors
    !*/
    weatherstats::weatherstats()
    {
        dbl_rain = 0;
        dbl_snow = 0;
        dbl_sunnyDays = 0;
    }

    weatherstats::weatherstats(double r, double s, double d)
    {
        dbl_rain = r;
        dbl_snow = s;
        dbl_sunnyDays = d;
    }

    /*!
     Accessors
    !*/
    double weatherstats::getRain()
    {
        return dbl_rain;
    }

    double weatherstats::getSnow()
    {
        return dbl_snow;
    }

    double weatherstats::getsunnyDays()
    {
        return dbl_sunnyDays;
    }

    /*!
     Mutators
    !*/
    void weatherstats::setRain(double r)
    {
        dbl_rain = r;
    }

    void weatherstats::setSnow(double s)
    {
        dbl_snow = s;
    }

    void weatherstats::setsunnyDays(double d)
    {
        dbl_sunnyDays = d;
    }

//Overload Opperator
ostream& operator << (ostream &stream, weatherstats &obj)
{
    stream <<&weatherstats::getRain << " - " << &weatherstats::dbl_snow << " - " << &weatherstats::getsunnyDays << endl;
    return stream;
}

2 个答案:

答案 0 :(得分:3)

我会在黑暗中用它刺伤,因为我没有触及模板或超载几年。

您的<<课程是否重载weatherstats运算符?

您的错误行正在尝试打印value成员变量。在ListNode定义中,您说value的类型为T(即模板)。

main()中,您要创建一个链接列表,其模板类型为weatherstats。 您的错误还指出它无法转换weatherstats类型。

所以问题是:您是否为<<重载weatherstats

很遗憾,您还没有发布此课程的代码,因此我们无法继续这样做。 编辑:代码已发布 - 仍然没有超载的证据

(另外我认为Baget稍后就你的流媒体运营商的方向提出了一个很好的观点)

EDIT2 :你应该如何调用函数?是&classname::function还是object.function()

答案 1 :(得分:0)

不需要stream >> obj.value; stream<<obj.value;

OUTPUT 流不是 INPUT