简体   繁体   中英

error: non-static reference member 'std::ostream& Student::out', can't use default assignment operator

I'm a bit of a novice C++ programmer and while working on a code I ran into this error:

C:\Users\Matt\Desktop\C++ Projects\OperatorOverload\students.h|8|error: non-static reference member 'std::ostream& Student::out', can't use default assignment operator|

The error was for line 8 of this header file:

#ifndef STUDENTS_H 
#define STUDENTS_H

#include <string>
#include <vector>
#include <fstream>

class Student {
private:
  std::string name;
  int credits;
  std::ostream& out;

public:
  // Create a student with the indicated name, currently registered for
  //   no courses and 0 credits
  Student (std::string studentName);

  // get basic info about this student
 std::string getName() const;
  int getCredits() const;

  // Indicate that this student has registered for a course
  // worth this number of credits
  void addCredits (int numCredits);
  void studentPrint(std::ostream& out) const;


};
inline
  std::ostream& operator<< ( std::ostream& out, const Student& b)
  {
      b.studentPrint(out);
      return out;
  }
  bool operator== ( Student n1, const Student&  n2)
  {

      if((n1.getCredits() == n2.getCredits())&&(n1.getName() == n2.getName()))
      {
          return true;
      }
      return false;
  }
  bool operator< (Student n1, const Student& n2)
  {
      if(n1.getCredits() < n2.getCredits()&&(n1.getName() < n2.getName()))
      {
          return true;
      }
      return false;
  }

#endif

The thing is I'm not quite sure what the error means, nor do I know how to fix it. Does anyone have a possible solution?

The problem with the code is, clearly, the std::ostream& member in your class. Semantically, I doubt that it actually makes sense to have this member. However, let's assume for a moment that you want to keep it. There are a few implications:

  1. Any user-defined constructor needs to explicitly initialize the reference in its member initializer list. The compiler will refuse to accept the constructor otherwise.
  2. The compiler won't be able to create an assignment operator because it doesn't know what should happen when assigning reference.

The error message seems to be about the assignment operator. You can work around this problem by defining an assignment operator explicitly, eg

Student& Student::operator= (Student const& other) {
    // assign all members necessary here
    return *this;
}

However, a better solution is to drop the reference parameter. You probably don't need it anyway: there are few classes for which it makes sense to store an std::ostream& member. Most of the time any stream is an ephemeral entity, temporarily used to send objects to or to receive objects from.

Somewhere in your code, you are using the assignment operator on one of your Student objects. But you haven't specially defined the assignment operator, you are just using the compiler-generated one. But the compiler-generated assignment operator does not work when you have reference members. Either disable the assignment operator (by making it private, or deleting it), or make the ostream member a pointer, instead of a reference. This is all assuming you really need this ostream object in your class, which I find suspect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM