【问题标题】:Error C2679: binary '<<' : no operator found (vector iterators used)错误 C2679:二进制“<<”:未找到运算符(使用了向量迭代器)
【发布时间】:2015-07-06 08:42:56
【问题描述】:

完整的错误信息如下:

错误 C2679:二进制“ > > 类型的右侧操作数的运算符 (或没有可接受的转换)

不是最漂亮的,但这是我的错误代码,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;}

错误发生在“*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;
};

我想知道如何解决这个问题。我知道我应该重载

任何帮助将不胜感激!

【问题讨论】:

  • How do I ask a good question? "包含足够的代码以允许其他人重现问题。有关此问题的帮助,请阅读 How to create a Minimal, Complete, and Verifiable example."。 “在询问由代码引起的问题时,如果您提供人们可以用来重现问题的代码,您将获得更好的答案。”
  • 您需要为项目类重载operator &lt;&lt;。这是一个MSDN 示例
  • 嗯,编译器错误信息很清楚。如果你想拥有这个功能,你需要提供你自己的实现。我不是开箱即用的。
  • 我认为你错过了最后一部分。 “我知道我应该重载

标签: c++ operator-overloading operator-keyword cout ostream


【解决方案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 对象上使用的,而不是在您的对象上。

【讨论】:

  • 可能希望projects&amp; 成为const
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
相关资源
最近更新 更多