An operator template??

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
 
S

Simon Saunders

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)
{
....

}
}
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

The compiler should have told you that operator= can only be a non-static
member function.
 
J

Jerry Krinock

member function.

Yes, that rings a bell in my brain. So it seems I can't really structure it
this way. So, here is what I did. I removed the std::complex inheritance
from MoreComplex<T>, and instead made it a base class, and instead added to
it a member called "value" of type complex<T>. Also, I removed the "friend"
declaration so that the overloaded operator = is now simply a "loose"
operator (not in the class, not even a friend).

I don't quite understand it all yet, but it compiles and links.

Thanks!

Jerry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,831
Members
47,374
Latest member
anuragag27

Latest Threads

Top