我正在编写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终端上会抛出上述错误。
答案 0 :(得分:2)
error: no match for 'operator<<' in 'os << study->Student::data'
note: candidates are: std::ostream& operator<<(std::ostream&, const Student&)
您要求它在流中写入int。请注意可能候选人的非常短的列表,编译器说它只知道如何将一个学生写入流。在您评论Student(int)构造函数之前,这曾经是可能的。该构造函数可用于将int转换为Student。当堆栈爆炸时,这将在运行时达到非常糟糕的结束,但这不是重点。
您缺少声明运算符的标头的#include&lt;&lt;允许将int写入流中。实际上不确定可能是哪一个,我不喜欢流。不是问题,家庭作业问题不应该有真正的答案:)