错误 C2679:二进制“<<”:未找到运算符(使用了向量迭代器)

Error C2679: binary '<<' : no operator found (vector iterators used)

完整的错误消息如下:

错误 C2679:二进制“<<”: 未找到采用 right-hand 类型 'std::_Vector_iterator< std ::_Vector_val < std::_ Simple _ types < projects > > >' 操作数的运算符 (或者没有可接受的转换)

不是最漂亮的,但这是我的错误代码,吸气剂和 setter 函数发生在其他地方,但它们对这个错误似乎并不重要:

#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”之前的“<<”处。

这里是对应这部分代码的header

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;
};

我想知道如何解决这个问题。我知道我应该重载 << 运算符,但我不知道在这种情况下该怎么做!

如有任何帮助,我们将不胜感激!

您必须在 ostream 对象和您自己的对象之间重载运算符<<。

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

此外,如果重载运算符需要访问其私有数据成员,则需要将此友元定义添加到 class:

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

您不能将运算符重载为成员方法,因为该函数实际上是在 ostream 对象上使用,而不是在您的对象上使用。