I have an isEqualTo function which does a comparison of several data types through the use of templates. When I first had it all in the main.cpp file all was well, however once I split the Complex class into it's header and it's cpp file I'm getting an error stating, "argument list for class template "Complex" is missing". So of course I check my default constructor for class complex and here it is:
**main.cpp**
#include <iostream>
#include <string> objects
#include "Complex.h"
using namespace std;
template <class T>
class Complex<T>
bool isEqualTo(T a, T b)
{
if(a==b)
{
cout << a << " is EQUAL to " << b << endl;
return true;
}
else cout << a << " is NOT EQUAL to " << b << endl;
return false;
}
int main()
{
//Comparing Complex class
Complex<int> complexA(10, 5), complexB(10, 54), complexC(10, -5), complexD(-10, -5); //Creating complex class objects
cout << endl << endl << "******Comparing Complex Objects:****** \n";
isEqualTo(complexA, complexA); //true
isEqualTo(complexA, complexB); //false
isEqualTo(complexC, complexA); //false
isEqualTo(complexD, complexD); //true
}
**Complex.h**
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
template <class T>
class Complex
{
public:
//default constructor for class Complex
Complex(int realPart, int imaginaryPart) : real(realPart), imaginary(imaginaryPart)
//Overloading equality operator
bool operator==(const Complex &right) const //address of our cosnt right operand will have two parts as a complex data type, a real and imaginary
{
return real == right.real && imaginary==right.imaginary; //returns true if real is equal to BOTH of its parts right.real and right.imaginary
}
//overloading insertion operator
friend ostream &operator<<(ostream&, Complex<T>&);
private: //private data members for class Complex
int real; //private data member real
int imaginary; //private data member imaginary
};
#endif
**Complex.cpp**
#include "Complex.h"
#include <iostream>;
using namespace std;
template<class T>
ostream &operator<<(ostream& os, Complex,T.& obj)
{
if(obj.imaginary > 0)//If our imaginary object is greater than 0
os << obj.real << " + " << obj.imaginary << "i";
else if (obj.imaginary == 0) //if our imaginary object is ZERO
os << obj.real; //Then our imaginary does not exist so insert only the real part
else //If no other condition is true then imaginary must be negative so therefor
{
os << obj.real << obj.imaginary << "i"; //insert the real and the imaginary
}
return os; //return the ostream object
}
Complex is a template class. So it needs a template argument when constructed.
Complex<int> complexA(10, 5);
^^^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With