Note that there are some explanatory texts on larger screens.

plurals
  1. POoverloading operator+ (C++)
    primarykey
    data
    text
    <p>Hey so I am working on an overload of +, but when I try to do a simple call like</p> <p>statistician a,b,c;</p> <p>a = b+c; </p> <p>When I do the above call it crashes. and if i do an overload of =, it just returns a zero. here is some of my code. Thanks for the help guys!!!</p> <p>FYI next_number(double value), increases the dynamic array by 1, and puts that value in end of the array.</p> <pre><code>statistician operator+(const statistician&amp; left, const statistician&amp; right) { statistician temp; if(left.m_iArraySize == 0) { return right; } else if(right.m_iArraySize == 0) { return left; } else { statistician temp; for(int i =0; i&lt; left.m_iArraySize; i++) { temp.next_number(left.m_dSeqArray[i]); } for(int i =0; i&lt; right.m_iArraySize; i++) { temp.next_number(right.m_dSeqArray[i]); } return temp; } } </code></pre> <p>The Implementation of the class</p> <pre><code>#include "Statistician.h" #include &lt;iostream&gt; using namespace std; namespace main_savitch_2C { statistician::statistician() { m_iArraySize=0; m_dSeqArray = new double[1]; // Preset our list to 1 items m_dSeqArray[0] = 0; } statistician::~statistician() { delete[] m_dSeqArray; m_iArraySize=0; } statistician::statistician(const statistician &amp;s) { m_iArraySize=0; m_dSeqArray = new double[1]; // Preset our list to 1 items m_dSeqArray[0] = 0; } int statistician::ChangeArraySize(int iArraySize) { double *iTempArray; iTempArray = new double[m_iArraySize]; //Assert Data assert(iArraySize &gt;-1); // Copy the info of the array into a temp array for(int i = 0; i &lt; m_iArraySize-1; i++) { iTempArray[i] = m_dSeqArray[i]; } // iTempArray = m_dSeqArray; // delete the array delete[] m_dSeqArray; m_dSeqArray = new double[iArraySize]; // Copy the info of the temp array back into the orginal array variable for(int i = 0; i &lt; m_iArraySize; i++) { m_dSeqArray[i] = iTempArray[i]; } m_dSeqArray[iArraySize-1]=0; // Free our TempArray delete[] iTempArray; return 0; } //length int statistician::length() const { if(m_iArraySize == 0) {return 0;} return m_iArraySize; } //sum() double statistician::sum() const { if(m_iArraySize == 0) {return 0;} long double dSum=0; for(int i = 0; i &lt; m_iArraySize; i++) { dSum = dSum + m_dSeqArray[i]; } return dSum; } //mean() double statistician::mean() const { if(m_iArraySize == 0) {return 0;} double dSum,dMean; int iCounter; dSum=0; dMean=0; iCounter=0; for(int i = 0; i &lt; m_iArraySize; i++) { dSum = dSum + m_dSeqArray[i]; iCounter++; } // Check for Dividing by zero if(dSum == 0) { dMean = 0; } else { dMean = dSum/iCounter; } return dMean; } //reset() int statistician::reset() { delete[] m_dSeqArray; m_iArraySize = 0; m_dSeqArray = new double[1]; m_dSeqArray[0] = 0; return 0; } //next_number(i) int statistician::next_number(double iValue) { // Ensure that size of the array has not somehow become negative m_iArraySize++; if(m_iArraySize &gt; 1) { ChangeArraySize(m_iArraySize); m_dSeqArray[m_iArraySize-1] = iValue; } else { m_dSeqArray[m_iArraySize-1] = iValue; } return 0; } //next_number(int iValue) int statistician::next_number(int iValue) { m_iArraySize++; if(m_iArraySize &gt; 1) { ChangeArraySize(m_iArraySize); m_dSeqArray[m_iArraySize-1] = iValue; } else { m_dSeqArray[m_iArraySize-1] = iValue; } return 0; } //next_number(char iValue) in case a character is passed int statistician::next_number(char iValue) { cout &lt;&lt; "Invalid value passed to next_number!" &lt;&lt;endl; return 0; } //Minimum double statistician::minimum() const { if(m_iArraySize == 0) {return 0;} double dMinimum = m_dSeqArray[0]; for(int i = 0; i &lt; m_iArraySize; i++) { if(m_dSeqArray[i] &lt; dMinimum) { dMinimum = m_dSeqArray[i]; } } return dMinimum; } //Maximum double statistician::maximum() const { if(m_iArraySize == 0) {return 0;} double dMaximum = m_dSeqArray[0]; for(int i = 0; i &lt; m_iArraySize; i++) { if(m_dSeqArray[i] &gt; dMaximum) { dMaximum = m_dSeqArray[i]; } } return dMaximum; } //Operator Overloading /* statistician statistician::operator=( statistician&amp; left) { cout &lt;&lt; left.sum(); cout &lt;&lt; left.length(); cout &lt;&lt; this-&gt;sum(); if(this != &amp;left) { this-&gt;reset(); for (int i =0; i &lt; left.m_iArraySize; i++) { this-&gt;next_number(left.m_dSeqArray[i]); cout&lt;&lt;this-&gt;next_number(left.m_dSeqArray[i]) &lt;&lt;endl; cout&lt;&lt;left.next_number(left.m_dSeqArray[i]) &lt;&lt;endl; } } return *this; } */ statistician operator+(const statistician&amp; left, const statistician&amp; right) { statistician temp; if(left.m_iArraySize == 0) { return right; } else if(right.m_iArraySize == 0) { return left; } else { statistician temp; for(int i =0; i&lt; left.m_iArraySize; i++) { temp.next_number(left.m_dSeqArray[i]); } for(int i =0; i&lt; right.m_iArraySize; i++) { temp.next_number(right.m_dSeqArray[i]); } return temp; } } bool operator==( const statistician&amp; left,const statistician&amp; right) { cout &lt;&lt; "L length = : "&lt;&lt;left.length() &lt;&lt;" R Length: "&lt;&lt; right.length()&lt;&lt;endl; if(left.length() != right.length()) { return false; } cout &lt;&lt; "L minimum = : "&lt;&lt;left.minimum() &lt;&lt;" R minimum: "&lt;&lt; right.minimum()&lt;&lt;endl; if(left.minimum() != right.minimum()) { return false; } cout &lt;&lt; "L maximum = : "&lt;&lt;left.maximum() &lt;&lt;" R maximum: "&lt;&lt; right.maximum()&lt;&lt;endl; if(left.maximum() != right.maximum()) { return false; } cout &lt;&lt; "L sum = : "&lt;&lt;left.sum() &lt;&lt;" R sum: "&lt;&lt; right.sum()&lt;&lt;endl; if(left.sum() != right.sum() ) {return false; } cout &lt;&lt; "L mean = : "&lt;&lt;left.mean() &lt;&lt;" R mean: "&lt;&lt; right.mean()&lt;&lt;endl; if(left.mean() != right.mean() ) {return false; } return true; } } // End of Namespace </code></pre> <p>and the .h....</p> <pre><code>#ifndef StatClass #define StatClass #include &lt;cassert&gt; namespace main_savitch_2C { class statistician //With copy constructor { public: // Constructor statistician(); //Deconstructor ~statistician(); //Copy constructor statistician(const statistician &amp;s); //length int length() const; //sum() double sum() const; //mean() double mean() const; //reset() int reset(); //next_number(i) int next_number(double iValue); int next_number(int iValue); int next_number(char iValue); //Minimum double minimum() const; //Maximum double maximum() const; //Operator Overloading //statistician operator=(statistician&amp; left); friend statistician operator+(const statistician&amp; left, const statistician&amp; right); friend bool operator==(const statistician&amp; left, const statistician&amp; right); private: // Private Variables double *m_dSeqArray; int m_iArraySize; // Private Functions int ChangeArraySize(int iArraySize); }; }; #endif </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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