为什么程序的成功取决于是否声明了重载构造函数



我正在编写c++程序。这是学生班:

#include "Student.hpp"
#include "Home.hpp"
#include <string>
using namespace std;
/*
 * This is default constructor
 */
Student::Student(){
}
/*
 * This is copy constructor
 */
Student::Student(const Student& orig) {
   copy(orig);// invokes deep copy method
}
/*
 * This is a destructor
 */
Student::~Student() {
}
/*
 * This is operator = overloading method.
 * 
 * @param student. It is a reference to student class
 * @return Returns pointer to current class
 */
Student & Student::operator=(Student & student){
   if(this != &student){ // checks if they are referencing the same class.          
      copy(student);
   }
   return *this;
}
/*
 * This is setter
 * 
 * @param x The random integer number
 */
void Student::setValue(int x){
   data = x;
}
/*
 * The getter.
 * 
 * @return Returns integer digit
 */
int Student::getValue(){
   return data; 
}
/*
 * The copy method. It makes a deep copy of a current class.
 * 
 * @param orig. It contains a reference to student class
 */
void Student::copy(const Student &orig){
    if(this != &orig){
    // makes a copy of data member
       data = orig.data;      
    }
}

这是主要方法的片段

Student * array = new Student[objectSize];
    cout << "nOriginal array of Student type: "; 
    int i = 0; 
    for(int x = objectSize; x > 0; x--){
      array[i].setValue(x);
      cout << array[i] << " "; // prints the contents of original Student type array
       i++;
    }
    defaultObject.addition(array, objectSize); // invokes function to sort array of Student type

这是头文件:

#include <string>
using namespace std;
#ifndef STUDENT_HPP
#define STUDENT_HPP
class Student {
    friend ostream& operator<< (ostream& os, const Student& study){// overloads << operator for Student class      
        os << study.data; // the data you output       
    return os; 
   }
public:  
    Student(); // default constructor
   // Student(int data);// overloaded constructor
    Student(const Student& orig);// copy constructor
    virtual ~Student();// destructor
    Student & operator=(Student& student); // overloads = operator
    void setValue(int x);// setter
    int getValue();// getter
    void copy(const Student &orig);// copy method

    friend bool operator> (Student &first, Student &second){// overloads greater operator
       return first.data > second.data;
   }    
private:
    int data;// data member for storing Student's class contents
};
#endif  /* STUDENT_HPP */

问题是,当我在头文件中注释这行Student(int data);时,程序会抛出以下错误:

Student.hpp: In function `std::ostream& operator<<(std::ostream&, const Student&)':
In file included from Student.cpp:12:
Student.hpp:21: error: no match for 'operator<<' in 'os << study->Student::data'
Student.hpp:20: note: candidates are: std::ostream& operator<<(std::ostream&, const Student&)
make[2]: *** [build/Debug/Cygwin-Windows/Student.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 4s)

事实上,Student.cpp文件中的重载构造函数并没有定义,但如果声明在那里,那么NetBeans上的程序就可以工作,尽管在Linux终端上它会抛出上述错误。

error: no match for 'operator<<' in 'os << study->Student::data'
note: candidates are: std::ostream& operator<<(std::ostream&, const Student&)

您要求它向流写入一个int。请注意,可能的候选者列表很短,编译器说它只知道如何将Student写入流。在您对Student(int(构造函数进行注释之前,这曾经是可能的。该构造函数可用于将int转换为Student。当堆栈爆炸时,在运行时这将是一个非常糟糕的结局,但这与重点无关。

声明运算符<的标头缺少#include<允许将int写入流。我真的不确定那可能是哪一个,我不喜欢流。这不是问题,家庭作业问题不应该有真正的答案:(

最新更新