J
Jerry Krinock
Hi,
I'd like to make a class of complex numbers (real, imaginary) which has an
extra number (real, imaginary, extraFoo) and be able to use operators on
arrays of them. So I declared a template class "MoreComplex" which is
derived from std::complex. It looks real good, and compiles until I try to
call an operator. Then, I get a link error since it can't find the
operator.
I think that it's too abstract, with all these templates the compiler
doesn't know where to look. Can anyone suggest the _correct_ way to
accomplish what I'm trying to do? Do I need an explicit specialization?
Thanks,
Jerry
Here is my code which will not link:
************************
#include <iostream>
#include <complex>
#include <valarray>
using namespace std ;
template <class T>
class MoreComplex : public complex<T>
{
public:
float extraFoo ;
friend valarray<MoreComplex<T> > operator=(
valarray<MoreComplex<T> > leftFoo,
valarray<complex<T> > rightFoo) ;
} ;
template <class T>
valarray<MoreComplex<T> >
operator=(
valarray<MoreComplex<T> > leftFoo,
valarray<complex<T> > rightFoo)
{
leftFoo.resize(rightArg.size()) ;
for (int i=0; i<rightArg.size(); i++)
{
leftFoo = complex<T>(real(rightFoo,imag(rightFoo)) ;
left.extraFoo = 0.0 ;
}
return leftFoo ;
}
int main ()
{
valarray<complex<float> > y(5) ;
// The above creates 5-element array of complex numbs
valarray<MoreComplex<float> > x ;
y = 2 ; // Sets all five elements to 2+i0
x = y ; // I want that to use my overloaded = operator
}
*******************
RESULT:
Link Error : undefined: 'operator=(std::valarray<MoreComplex<float> >,
valarray<std::complex<float> >)' (code)
Referenced from 'main' in main.cpp
I'd like to make a class of complex numbers (real, imaginary) which has an
extra number (real, imaginary, extraFoo) and be able to use operators on
arrays of them. So I declared a template class "MoreComplex" which is
derived from std::complex. It looks real good, and compiles until I try to
call an operator. Then, I get a link error since it can't find the
operator.
I think that it's too abstract, with all these templates the compiler
doesn't know where to look. Can anyone suggest the _correct_ way to
accomplish what I'm trying to do? Do I need an explicit specialization?
Thanks,
Jerry
Here is my code which will not link:
************************
#include <iostream>
#include <complex>
#include <valarray>
using namespace std ;
template <class T>
class MoreComplex : public complex<T>
{
public:
float extraFoo ;
friend valarray<MoreComplex<T> > operator=(
valarray<MoreComplex<T> > leftFoo,
valarray<complex<T> > rightFoo) ;
} ;
template <class T>
valarray<MoreComplex<T> >
operator=(
valarray<MoreComplex<T> > leftFoo,
valarray<complex<T> > rightFoo)
{
leftFoo.resize(rightArg.size()) ;
for (int i=0; i<rightArg.size(); i++)
{
leftFoo = complex<T>(real(rightFoo,imag(rightFoo)) ;
left.extraFoo = 0.0 ;
}
return leftFoo ;
}
int main ()
{
valarray<complex<float> > y(5) ;
// The above creates 5-element array of complex numbs
valarray<MoreComplex<float> > x ;
y = 2 ; // Sets all five elements to 2+i0
x = y ; // I want that to use my overloaded = operator
}
*******************
RESULT:
Link Error : undefined: 'operator=(std::valarray<MoreComplex<float> >,
valarray<std::complex<float> >)' (code)
Referenced from 'main' in main.cpp