[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
Re: Re: problems with templates.
Attached is a version of Complex.hpp with the corrections with which I could
run the program.
Basically, when you define a function using templates, you must add
"template<class T>" in front, even for a friend method. Also, friend keyword
should be specified when declaring a method but not when defining it.
Nice program by the way.
Ouriel
-- Ouriel Grynszpan Software Engineer GlobeID Tél. + 33 (0)1 56 54 19 58 Fax
+ 33 (0)1 56 54 19 19 Email: ouriel@xxxxxxxxxxx Web site : www.globeid.com
----- Original Message -----
From: "Vilas Kumar Chitrakaran" <cvilas@xxxxxxxxxxx>
To: "Shridhar Daithankar" <shridhard@xxxxxxxxxx>; "linux-india-programmers"
<linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx>
Sent: Tuesday, October 31, 2000 6:15 PM
Subject: [LIP] Re: problems with templates.
> The error messages are in the attached file...
>
>
> > What's the exact error message?
> >
> > Bye
> > Shridhar
> >
> > Vilas Kumar Chitrakaran wrote:
> >
> > > Hi programmers,
> > >
> > > I am quite a novice C++ programmer trying to make some sense out of
> > > templates. I have atached a program here that compiles well in
MSvisual
> C++
> > > but does not compile in GNU C++ compiler.
> >
> >
> >
>
----------------------------------------------------------------------------
----
> ---------------------------------------------
> The mailing list archives are available at
> http://lists.linux-india.org/cgi-bin/wilma/LIP
>
//==================================================================
// Project : Library for Complex Numbers
//------------------------------------------------------------------
// Package :
// Authors : Villi
// Start date :
// Compiler : GNU C++
// Operating System : any
//------------------------------------------------------------------
// File : Complex.hpp
// Header file for the class Complex.
//==================================================================
#include<iostream.h>
#include<iomanip.h>
#include<stdio.h>
#include<math.h>
//==================================================================
//
// class Complex
//------------------------------------------------------------------
// The following is a definition of class template for complex numbers.
// Class methods include functions to get/set real and imaginary parts
// of the complex number.
//
// The arithmetic and istream/ostream operators are overloaded to
// allow direct manipulation on the data of type Complex just as
// built in types.
//==================================================================
template <class T>
class Complex
{
public:
Complex(){ d_realPart=0; d_imaginaryPart=0;}
// The default Constructor
Complex(T realPart, T imaginaryPart){d_realPart=realPart; d_imaginaryPart=imaginaryPart; }
// A Constructor that allows the user to initialise
// the data type
~Complex() {}
// The Destructor
friend ostream &operator<<(ostream&, const Complex<T> &);
// Overloading ostream operator << to directly accept complex type
friend istream &operator>>(istream&, Complex<T> &);
// Overloading istream operator >> to directly accept complex type
Complex operator+(const Complex<T> &);
// Overloading binary operator +
// enables sum = C1 + C2 where C1 and C2 are type Complex
Complex operator+=(const Complex<T> & secondNumber);
// Overloading the += operator for operations with class complex
Complex operator-(const Complex<T> &);
// Overloading binary operator -
// enables difference = C1 - C2 where C1 and C2 are type Complex
Complex operator-=(const Complex<T> & secondNumber);
// Overloading the -= operator for operations with class complex
Complex operator*(const Complex<T> &);
// Overloading binary operator *
// enables product = C1 * C2 where C1 and C2 are type Complex
Complex operator*=(const Complex<T> & secondNumber);
// Overloading the *= operator for operations with class complex
Complex operator/(const Complex<T> &);
// Overloading binary operator /
// enables division = C1 / C2 where C1 and C2 are type Complex
Complex operator/=(const Complex<T> & secondNumber);
// Overloading the /= operator for operations with class complex
T getRealPart() const { return d_realPart; }
// Obtains the real part of the complex number
T getImaginaryPart() const { return d_imaginaryPart; }
// Obtains the imaginary part of the complex number
void setRealPart(T real) {d_realPart = real; }
// Sets the real part of the complex number
void setImaginaryPart(T imag) {d_imaginaryPart = imag; }
// Sets the imaginary part of the complex numbe
private:
T d_realPart;
//real part of the complex number
T d_imaginaryPart;
//imaginary value of the complex number
};
//==================================================================
// The remaining part of this file gives the definitions for all the
// functions declared above.
//==================================================================
template<class T>
/*friend*/ ostream &operator<<(ostream &output, const Complex<T> &num)
// This function overloads the << ostream operator
// so that 'cout' accepts the data type 'complex'
// enables cout << x <<"+ j" << y
{
output << num.d_realPart << " + j " << "(" << num.d_imaginaryPart << ")";
return output;
}
template<class T>
/* friend*/ istream &operator>>(istream &input, Complex<T> &num)
// This function overloads the >> istream operator
// so that 'cin' accepts the data type 'complex'
// enables cin >> X+ jY
{
while((cin.peek()=='\n')||(cin.peek()=='\r'))
cin.ignore(1);
input.ignore(1);
//ignore '('
input >> setw(4) >> num.d_realPart;
//input real part in 3 digits or more
input.ignore(1);
//skip (,)
input >> setw(4) >> num.d_imaginaryPart;
//input imaginary part in 3 digits or more
input.ignore(1);
//ignore ')'
return input;
}
template<class T>
Complex<T> Complex<T>::operator+(const Complex<T> & secondNumber)
// Overloading operator + to directly operate on complex numbers
// enables sum = C1 + C2 where C1 and C2 are type Complex
{
Complex<T> sum;
sum.setRealPart(getRealPart() + secondNumber.getRealPart());
sum.setImaginaryPart(getImaginaryPart() + secondNumber.getImaginaryPart());
return (sum);
}
template<class T>
Complex<T> Complex<T>::operator+=(const Complex<T> & secondNumber)
// Overloading the += operator for operations with class complex
{
*this=*this + secondNumber;
return *this;
}
template<class T>
Complex<T> Complex<T>::operator-(const Complex<T> & secondNumber)
// Overloading operator - to directly operate on complex numbers
// enables difference = C1 - C2 where C1 and C2 are type Complex
{
Complex<T> difference;
difference.setRealPart(getRealPart() - secondNumber.getRealPart());
difference.setImaginaryPart(getImaginaryPart() - secondNumber.getImaginaryPart());
return (difference);
}
template<class T>
Complex<T> Complex<T>::operator-=(const Complex<T> & secondNumber)
// Overloading the -= operator for operations with class complex
{
*this=*this - secondNumber;
return *this;
}
template<class T>
Complex<T> Complex<T>::operator *(const Complex<T> & secondNumber)
// Overloading operator * to directly operate on complex numbers
// enables product = C1 * C2 where C1 and C2 are type Complex
{
Complex product;
product.setRealPart(getRealPart()*secondNumber.getRealPart() - getImaginaryPart()*secondNumber.getImaginaryPart());
product.setImaginaryPart(getRealPart()*secondNumber.getImaginaryPart() + getImaginaryPart()*secondNumber.getRealPart());
return (product);
}
template<class T>
Complex<T> Complex<T>::operator*=(const Complex<T> & secondNumber)
// Overloading the += operator for operations with class complex
{
*this=*this * secondNumber;
return *this;
}
template<class T>
Complex<T> Complex<T>::operator /(const Complex<T> & secondNumber)
// Overloading operator / to directly operate on complex numbers
// enables sum = C1 / C2 where C1 and C2 are type Complex
{
Complex<T> division;
division.setRealPart((getRealPart()*secondNumber.getRealPart() + getImaginaryPart()*secondNumber.getImaginaryPart())/(secondNumber.getRealPart()*secondNumber.getRealPart() + secondNumber.getImaginaryPart()*secondNumber.getImaginaryPart()));
division.setImaginaryPart((getImaginaryPart()*secondNumber.getRealPart() - getRealPart()*secondNumber.getImaginaryPart())/(secondNumber.getRealPart()*secondNumber.getRealPart() + secondNumber.getImaginaryPart()*secondNumber.getImaginaryPart()));
return (division);
}
template<class T>
Complex<T> Complex<T>::operator/=(const Complex<T> & secondNumber)
// Overloading the += operator for operations with class complex
{
*this=*this / secondNumber;
return *this;
}