I am writing c++ program. This is Student class:
#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;
}
}
This is snippet of main method:
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
This is header file:
#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 */
The problem is that when I comment this line Student(int data);
in header file the program throws this error:
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)
In fact that overloaded constructor at Student.cpp file is not defined, but if declaration is there the program on NetBeans works, though on Linux terminal it throws the mentioned error.
setValue
,getValue
,copy
, etc. in your code. Do you really need those to exhibit your problem? Do you need the loop in main? Wouldn't a simpleStudent s; std::cout << s;
suffice? – Xeo Dec 7 '11 at 0:11std::vector
and other standard library containers instead of raw arrays and pointers; (2) don't express copy construction in terms of assignment, instead express assignment in terms of copy construction (look up the "swap idiom"); (3) when you're talking about "deep copying" chances are you need a destructor (look up the "rule of three"). Cheers & hth., – Cheers and hth. - Alf Dec 7 '11 at 0:40