如果声明了重载构造函数,为什么程序的成功依赖于此? 我正在编写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.cpp文件中的重载构造函数并没有定义,但如果有声明,NetBeans上的程序就会工作,尽管在Linux终端上会抛出上述错误如果声明了重载构造函数,为什么程序的成功依赖于此? 我正在编写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; } },c++,netbeans,C++,Netbeans,事实上,Student.cpp文件中的重载构造函数并没有定义,但如果有声明,NetBeans上的程序就会工作,尽管在Linux终端上会抛出上述错误 错误:与“运算符”不匹配请提供显示您的问题的。尽量简短。@ucas:不,不是。代码中应该不需要setValue、getValue、copy等。你真的需要这些来证明你的问题吗?你需要主回路吗?难道不是一个简单的学生;std::cout@ucas:您需要这样做来触发编译器错误吗?您能显示您包含的标准头吗?当前显示的代码与编译器的诊断不匹配。在一个小程序中
错误:与“运算符”不匹配请提供显示您的问题的。尽量简短。@ucas:不,不是。代码中应该不需要setValue
、getValue
、copy
等。你真的需要这些来证明你的问题吗?你需要主回路吗?难道不是一个简单的学生;std::cout@ucas:您需要这样做来触发编译器错误吗?您能显示您包含的标准头吗?当前显示的代码与编译器的诊断不匹配。在一个小程序中重现错误,并保持该程序不变。一些一般建议:(1)优先使用std::vector
和其他标准库容器,而不是原始数组和指针;(2) 不要用赋值来表示复制构造,而要用复制构造来表示赋值(查阅“交换习语”);(3) 当你谈论“深度复制”时,你很可能需要一个析构函数(查阅“三法则”)。干杯,它应该在
中,如果您想使用std::ostream
(或者他只是包含了
,它只是转发声明了所有模板),那么应该包含在
中。在Windows上的NetBeans上,此解决方案可以工作,但在Linux命令行上,它会抛出错误“未定义对Assessment::Assessment()的引用”。
error: no match for 'operator<<' in 'os << study->Student::data'
note: candidates are: std::ostream& operator<<(std::ostream&, const Student&)
#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.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)
error: no match for 'operator<<' in 'os << study->Student::data'
note: candidates are: std::ostream& operator<<(std::ostream&, const Student&)