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[i] = complex<T>(real(rightFoo[i],imag(rightFoo[i])) ;
left[i].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 2 5387
On Sat, 01 Nov 2003 05:16:28 +0000, Jerry Krinock wrote: 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.
in article pa****************************@net.ntl.com, Simon Saunders at si************@net.ntl.com wrote on 03/11/01 02:45: The compiler should have told you that operator= can only be a non-static 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: YUY0x7 |
last post by:
Hi, I am having a bit of trouble with a specialization of operator<<.
Here goes:
class MyStream
{
};
template <typename T>
MyStream& operator<<(MyStream& lhs, T const &)
|
by: hall |
last post by:
Hi all.
I have run into a problem of overloading a templatized operator>> by a
specialized version of it. In short (complete code below), I have
written a stream class, STR, which defines a...
|
by: Atlas |
last post by:
Hi,
I implemented a template as:
template <int L, int M, int T>
class Quantity
{
.....
public:
friend Quantity operator*(const Quantity& q1,const Quantity& q2);
.....
};
|
by: gugdias |
last post by:
I'm coding a simple matrix class, which is resulting in the following
error when compiling with g++ 3.4.2 (mingw-special):
* declaration of `operator/' as non-function
* expected `;' before '<'...
|
by: Harry |
last post by:
Hi all,
I am writing a logger program which can take any datatype.
namespace recordLog {
enum Debug_Level {low, midium, high};
class L {
std::ofstream os;
Debug_Level cdl;
const...
|
by: Amadeus W. M. |
last post by:
What is the difference between
friend ostream & operator<<(ostream & OUT, const Foo & f){
// output f
return OUT;
}
and
template <class X>
|
by: Zilla |
last post by:
I have the following simple program. I just want to be able to do math
operations (+, -, =)on Timer sublcasses, but want to handle cases
where either rhs or lhs is an intrinsic value, However, the...
|
by: ruchirp |
last post by:
Hi, I've been trying to overload << for a function similar to boost lambda library.
I want to make for_each(v.begin(), b.end(), cout << _1 << endl); to work, however, i'm getting a lot of problems...
|
by: aaragon |
last post by:
Hi everyone,
I was unable to find out why my code is not compiling. I have a
template class and I'm trying to write the operator<< for standard
output. Does anyone know why this is not right?...
|
by: none |
last post by:
I'm trying to overload the = operator using templates, but I have
some problems with one of the overloads, I would like to make
something like that:
intvariable = fooclass;
here's a pseudo...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |