完整的错误消息显示为:
错误C2679:二进制'<<' :找不到运算符,它采用类型'std :: _ Vector_iterator<的右手操作数std :: _ Vector_val< std :: _ Simple _ types<项目> > >” (或者没有可接受的转换)
不是最漂亮的,但这是我的代码直到错误,getter和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;}
错误发生在“&lt;&lt;”的最后一行在“* 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;
};
我想知道如何解决这个问题。我明白我应该重载&lt;&lt;操作员,但我不知道如何做这个案子!
非常感谢任何帮助!
答案 0 :(得分:1)
您必须重载运算符&lt;&lt;在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对象,而不是对象。