简体   繁体   English

错误:非静态引用成员'std :: ostream&Student :: out',不能使用默认赋值运算符

[英]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 ++新手,在编写代码时遇到了以下错误:

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: 错误是此头文件的第8行的:

#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. 显然,代码的问题是类中的std::ostream&成员。 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. 无论如何,您可能根本不需要它:对于几个存储std::ostream&成员有意义的类。 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. 在代码中的某个位置,您正在对一个Student对象使用赋值运算符。 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. 禁用赋值运算符(通过将其设为私有或删除它),或将ostream成员设为指针而不是引用。 This is all assuming you really need this ostream object in your class, which I find suspect. 所有这些都假设您确实在您的类中需要此ostream对象,我对此感到怀疑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:非静态引用成员 - 不能使用默认赋值运算符 - error: non-static reference member - can't use default assignment operator 错误:非静态引用成员,不能使用默认赋值运算符 - Error: non-static reference member, can't use default assignment operator 非静态参考成员&#39;int&Property <int> :: value&#39;,不能使用默认赋值运算符 - non-static reference member ‘int& Property<int>::value’, can’t use default assignment operator '非静态引用成员,不能使用默认赋值运算符' - 'non-static reference member, can't use default assignment operator' &#39;类型非静态const成员不能使用默认赋值运算符&#39; - 这是什么意思? - 'Type non-static const member can't use default assignment operator' - what does this mean? 非静态 const 成员,不能使用默认赋值运算符 - Non-static const member, can't use default assignment operator 错误:“对运算符&lt;&lt;(std :: ostream&,Dogs const&)的未定义引用” - Error: “Undefined reference to operator<<(std::ostream&, Dogs const&)” 朋友ostream&运营商&lt;&lt;无法访问私人会员 - Friend ostream& operator<< can't access private member “friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, LinkedList&amp; list)”是什么意思? - What does “friend std::ostream& operator<<(std::ostream& out, LinkedList& list)” mean? 未定义对 `operator&lt;&lt;(std::ostream&amp;, /* class 与非类型模板参数 */&amp;)' 的引用 - undefined reference to `operator<<(std::ostream&, /* class with non-type template parameters */&)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM