gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:43:31 25 4
gpt4 key购买 nike

我正在编写 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&)

您要求它向流中写入一个整数。注意可能的候选人名单非常短,编译器说它只知道如何将 Student 写入流。在您评论 Student(int) 构造函数之前,这曾经是可能的。该构造函数可用于将 int 转换为 Student。当堆栈爆炸时,这将在运行时出现非常糟糕的结局,但这不是重点。

您缺少一个 header 的#include,该 header 声明了一个允许将 int 写入流的运算符<<。实际上不确定可能是哪一个,我不喜欢流。没问题,家庭作业问题不应该有真正的答案:)

关于c++ - 为什么程序成功取决于是否声明了重载构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8408646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com