2015-04-27 42 views
-5

完整的錯誤信息爲:錯誤C2679:二進制「<<」:沒有操作員發現(用於矢量迭代器)

錯誤C2679:二進制「< <」:沒有操作員發現這需要的右邊的操作數鍵入 '的std :: _ Vector_iterator <的std :: _ Vector_val <的std :: _簡單_類型<項目>>>' (或沒有可接受的轉換)

不是最漂亮的,但這裏是我的代碼錯誤產生,在其他地方發生吸氣和吸氣功能,但它們對於這個錯誤似乎並不重要:

#include "Project.hpp" 
#include <iostream> 
#include <sstream> 
#include <string> 
#include <fstream> 
#include <list> 
#include <ostream> 
#include <stdlib.h> 
using namespace std; 

int main(){ 


cout << "Welcome to our project topic allocator, please enter the student spreadsheet; " << endl; 
vector<student> studentvector; 
vector<projects> projectsvector; 
vector<projects> availchoicevector; 

//string excelsupervisor; 
//supervisor staff; 

string excelstudent; 
student pupil; 

string excelprojects; 
projects selections; 

selections.zbbb(excelprojects); 
//pupil.xbbb(excelstudent); 
//staff.ybbb(excelsupervisor); 
int counter; 


vector<student>::iterator first1 = studentvector.begin(), last1 = studentvector.end(), fileline1; 

vector<projects>::iterator first2 = projectsvector.begin(), last2 = projectsvector.end(), fileline2; 

for (fileline1 = studentvector.begin(), fileline2 = projectsvector.begin(); fileline1 != studentvector.end(), fileline2 != projectsvector.end(); fileline1++, fileline2++){ 

    cout << "Student ID  " << fileline1->get_studentname() << endl; 
    int var1 = fileline1->get_numberofselections(); 

    if (var1 = !4){ 

     cout << "Student has not made 4 choices, consult student!" << endl; 
     fileline2 = fileline2 + (var1 - 1); 
     continue; 
    } 
    else{ 
     //for (fileline2; fileline2 < fileline2 + 4; fileline2++){ 
     const auto &p = fileline2->get_projectID(); //fileline2 = 1st choice 
     auto y = find(availchoicevector.begin(), availchoicevector.end(), p); 
     if (y != availchoicevector.end()){ 
      cout << "Project ID . Supervisor ID of allocated choice: " << *y << "." << fileline2->get_supervisorIDproj << endl;} 

由於在「* y」之前的「< <」上的最後一行發生錯誤。

下面是對應於這部分代碼的標頭。

class projects{ 

private: 
string projectname, projectsup, supervisornameproj, stunameproj; 
float projectID; 
int itterator, rank, classs, supervisorIDproj, stuIDproj, regnumproj; 



public: 





projects(); 
~projects(); 
void set_projectname(string); 
void set_supervisornameproj(string); 
void set_stunameproj(string); 

void set_stuIDproj(int); 
void set_supervisorIDproj(int); 
void set_projectID(int); 
void set_regnumproj(int); 
void set_rank(int); 
void set_classs(int); 


string get_projectname(){return projectname;} 
string get_stunameproj(){return stunameproj;} 
string get_supervisornameproj(){return supervisornameproj;}  

int get_regnumproj(){return regnumproj;} 
int get_supervisorIDproj(){return supervisorIDproj;} 
int get_stuIDproj(){return stuIDproj;} 
int get_projectID(){return projectID;} 
int get_rank(){return rank;} 
int get_classs(){return classs;} 


bool zbbb(string); 
bool sorting(int); 

vector<projects> projectsvector; 
vector<projects> availchoicevector; 
}; 

我想知道如何解決這個問題。我知道我應該重載< <運營商,但我不知道如何爲這種情況下做到這一點!

任何幫助將不勝感激!

+0

[我如何問一個好問題?](http://stackoverflow.com/help/how-to-ask)「只包含足夠的代碼以允許其他人重現問題。有關此問題的幫助,請閱讀[如何創建一個最小,完整和可驗證的示例。](http://stackoverflow.com/help/mcve)「。 「當問一個關於你的代碼引起的問題的問題時,如果你提供的代碼可以用來重現問題,你將得到更好的答案。」 –

+2

您需要重載項目類的'operator <<'。這是一個[MSDN](https://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx?f=255&MSPPError=-2147217396)示例 – NathanOliver

+1

嗯,編譯器的錯誤信息很清楚。如果你想擁有這個功能,你需要提供你自己的實現。我不是從盒子裏出來的。 –

回答

1

您必須在ostream對象和您自己的對象之間重載運算符< <。

ostream &operator<<(ostream &output, const projects &prj) 
{ 
    //print something you want of prj with output << ... 
    return output; 
} 

此外,你需要這個朋友的定義,如果重載操作需要訪問它的私有數據成員添加到類:

friend ostream &operator<<(ostream &, const projects &); 

不能運算符重載爲成員方法,因爲這個函數實際上是用在一個ostream對象上,而不是在你的對象上。

+1

可能需要'項目&'爲'const'。 –

相關問題