Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does the program success depend on that if overloaded constructor is declared?
    text
    copied!<p>I am writing c++ program. This is Student class:</p> <pre><code>#include "Student.hpp" #include "Home.hpp" #include &lt;string&gt; using namespace std; /* * This is default constructor */ Student::Student(){ } /* * This is copy constructor */ Student::Student(const Student&amp; 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 &amp; Student::operator=(Student &amp; student){ if(this != &amp;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 &amp;orig){ if(this != &amp;orig){ // makes a copy of data member data = orig.data; } } </code></pre> <p><strong>This is snippet of main method</strong>:</p> <pre><code>Student * array = new Student[objectSize]; cout &lt;&lt; "\nOriginal array of Student type: "; int i = 0; for(int x = objectSize; x &gt; 0; x--){ array[i].setValue(x); cout &lt;&lt; array[i] &lt;&lt; " "; // prints the contents of original Student type array i++; } defaultObject.addition(array, objectSize); // invokes function to sort array of Student type </code></pre> <p>This is header file:</p> <pre><code>#include &lt;string&gt; using namespace std; #ifndef STUDENT_HPP #define STUDENT_HPP class Student { friend ostream&amp; operator&lt;&lt; (ostream&amp; os, const Student&amp; study){// overloads &lt;&lt; operator for Student class os &lt;&lt; study.data; // the data you output return os; } public: Student(); // default constructor // Student(int data);// overloaded constructor Student(const Student&amp; orig);// copy constructor virtual ~Student();// destructor Student &amp; operator=(Student&amp; student); // overloads = operator void setValue(int x);// setter int getValue();// getter void copy(const Student &amp;orig);// copy method friend bool operator&gt; (Student &amp;first, Student &amp;second){// overloads greater operator return first.data &gt; second.data; } private: int data;// data member for storing Student's class contents }; #endif /* STUDENT_HPP */ </code></pre> <p>The problem is that when I comment this line <code>Student(int data);</code> in header file the program throws this error:</p> <pre><code>Student.hpp: In function `std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const Student&amp;)': In file included from Student.cpp:12: Student.hpp:21: error: no match for 'operator&lt;&lt;' in 'os &lt;&lt; study-&gt;Student::data' Student.hpp:20: note: candidates are: std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const Student&amp;) 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) </code></pre> <p>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. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload