2014-04-20 43 views
0

我正在處理一些C++作業,並遇到了一個障礙,我無法在我的鏈表類中運行我的displayList()函數。我收到以下錯誤。C++沒有<<運算符找到需要右手操作數

錯誤1錯誤C2679:二進制「< <」:沒有操作員發現它接受一個右邊的操作數類型的「weatherstats」(或沒有可接受的轉換)C:\用戶\拉里\文件\視覺工作室2013 \項目\ weatherstats \ weatherstats \ linkedlist.h 100個1 weatherStats

所以我需要重載< <操作數,但我已經用我在網上找到,但沒有運氣的幾個例子嘗試。有人可以幫我用我的< <超載操作員嗎?

編輯:新增Weatherstats.cpp

編輯2:我創建了我的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; 
} 
+0

我沒有在您的列表中看到您正在使用<<運算符。 – Baget

+1

@Baget我試圖打印出我的節點值在我的displayList()函數linkedlist.h與cout << nodePtr->值<< endl; – Lgwells1

+0

你的問題現在解決了嗎? – DiJuMx

回答

3

我要採取刺傷在黑暗中與這一點,因爲我沒碰過模板或超載了幾年。

您是否有<<運算符爲您的weatherstats類重載?

您的錯誤行嘗試打印value成員變量。在您的ListNode定義中,您說value的類型爲T(即模板)。

在您的main()中,您正在創建一個鏈接列表,其模板類型爲weatherstats。 您的錯誤還表明它不能轉換weatherstats類型。

所以問題是:你是否超載<<weatherstats

不幸的是,您還沒有發佈此課程的代碼,所以我們不能再深入瞭解此課程。編輯:代碼已經公佈 - 仍然沒有超載

(另外我覺得Baget讓你流運營商的方向好點以後)

EDIT2的證據:你應該如何調用功能?是&classname::function還是object.function()

+0

DiJuMx,我添加了我的weatherstats.cpp文件,我沒有包含頭文件,如果你想看到,請讓我知道。 – Lgwells1

+1

就像我在我的回答中所說的,我看不到任何證據表明你已經爲'weatherstats'類重載了'<<'運算符。這是你的錯誤來自哪裏。如果它在頭文件中,那麼它會很有用。 – DiJuMx

+0

澄清一點:該錯誤與'class linkedlist '完全沒有直接關係。 'nodePtr-> value'的類型是'weatherstats',但是編譯器看不到像'operator <<(std :: ostream&,const weatherstats&)'這樣的函數。 – aschepler

0

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

它是OUTPUT流不INPUT

+0

我嘗試了您的建議,但仍然在同一位置收到同樣的錯誤。 – Lgwells1

相關問題