#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
template<class T>
class MyComplex {
public:
void setReal(T myreal);
void setImg(T myimg);
MyComplex();
MyComplex(T myreal);
MyComplex(T myreal, T myi);
// template<class U>
MyComplex<T> operator + (MyComplex<T> const& obj) {
MyComplex result;
result.realNum = realNum + obj.realNum;
result.iNum = iNum + obj.iNum;
return result;
}
MyComplex<T> operator - (MyComplex<T> const& obj) {
MyComplex result;
result.realNum = realNum - obj.realNum;
result.iNum = iNum - obj.iNum;
return result;
}
MyComplex<T> operator * (MyComplex<T> const& obj) {
MyComplex result;
result.realNum = realNum * obj.realNum;
result.iNum = iNum * obj.iNum;
return result;
}
MyComplex<T> operator / (MyComplex<T> const& obj) {
MyComplex result;
result.realNum = ((realNum * obj.realNum) + (iNum * obj.iNum)) / (pow(obj.realNum, 2) + pow(obj.iNum, 2));
result.iNum = ((iNum * obj.realNum) - (obj.iNum * realNum)) / (pow(obj.realNum, 2) + pow(obj.iNum, 2));
return result;
}
void print() {
if (iNum < 0) {
cout << realNum << " - i" << abs(iNum) << endl;
}
else {
cout << realNum << " + i" << iNum << endl;
}
}
void getInput() {
cout << "Enter the real number: " << endl;
cin >> realNum;
cout << "Enter the imaginary number: " << endl;
cin >> iNum;
}
// friend ostream& operator << (ostream& out, const MyComplex<T>& U);
friend ostream& operator << (ostream& out, const MyComplex& U);
// friend istream& operator >> (istream& in, MyComplex<T>& U);
friend istream& operator >> (istream& in, MyComplex& U);
private:
T realNum;
T iNum;
};
template <class T>
istream& operator >> (istream& in, MyComplex<T>& U)
{
cout << "Enter the Real number ";
in >> U.realNum;
cout << "Enter the Imaginary number ";
in >> U.iNum;
return in;
}
//default constructor
template <class T>
MyComplex<T>::MyComplex()
{
realNum = 0.0;
iNum = 0.0;
}
//single argument constructor
template <class T>
MyComplex<T>::MyComplex(T myreal)
{
realNum = myreal;
}
//double argument constructor
template <class T>
MyComplex<T>::MyComplex(T myreal, T myi)
{
realNum = myreal;
iNum = myi;
}
template <class T>
void MyComplex<T>::setReal(T myreal) {
realNum = myreal;
}
template <class T>
void MyComplex<T>::setImg(T myimg) {
iNum = myimg;
}
int main() {
MyComplex<double> num1;
MyComplex<double> num2, num3, num4, num5;
cin >> num1;
cin >> num2;
num1.print();
num2.print();
num5 = (num1 / num2);
num5.print();
return(0);
}
I am trying to do this code within a template format. However I keep getting this error code.
error: undefined symbol: _ZrsRNSt3__213basic_istreamIcNS_11char_traitsIcEEEER9MyComplexIdE (referenced by top-level compiled C/C++ code)
warning: Link with `-sLLD_REPORT_UNDEFINED` to get more information on undefined symbols
warning: To disable errors for undefined symbols use `-sERROR_ON_UNDEFINED_SYMBOLS=0` warning: __ZrsRNSt3__213basic_istreamIcNS_11char_traitsIcEEEER9MyComplexIdE may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
Error: Aborting compilation due to previous errors
I sent it to my professor and he said the following but I don't quite understand what he means, I don't really want to ask him again. Could someone help me with this? What does he mean
You need to refer to the class name as MyComplex.
I took out the two friend functions for i/o.
The operator >> should NOT prompt on cout. I should not assume input is coming from cin.
Perhaps (initially) writing the two operators as non-friend and using the accessor/mutator methods would help. Making them friends is an optimization. Get the code working first.
He sent me a "fixed" code but it still does not work,