473,805 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with operator overloading

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 compile
fails in my g++ 2.95 compiler during the 2nd to last line of the
main() with

template.cpp: In function `int main()':
template.cpp:24 : `operator +<int>(int, const int &)' must have an
argument of class or enumerated type
template.cpp: In method `int & Timer::operator +<int>(const int &)':
template.cpp:97 : instantiated from here
template.cpp:42 : request for member `_value' in `t', which is of non-
aggregate type `int'
template.cpp:43 : static_cast from `Timer *' to `int *'

Seems like the operator+(T& t) function is being called, instead of
the operator+(int) function. Why?
#include <iostream.h>

class Timer {
template<class T>
friend T& operator+(const int value, const T& t);
public:
Timer();
~Timer();

template<class T>
T& operator+(const T& t);
template<class T>
T& operator+(const int value);
template<class T>
T& operator=(const T& t);
template<class T>
T& operator=(const int value);

int _value;
};

template<class T>
T& operator+(const int value, const T& t)
{
static T temp(value);
temp._value=t._ value+value;
return temp;
}

Timer::Timer()
{
}

Timer::~Timer()
{
}

template<class T>
T& Timer::operator +(const T& t)
{
// T* This=dynamic_ca st<T*>(this);
this->_value+=t._val ue;
return *(static_cast<T *>(this));
}

template<class T>
T& Timer::operator +(const int value)
{
T* This=dynamic_ca st<T*>(this);
This->_value+=valu e;
return *This;
}

template<class T>
T& Timer::operator =(const T& t)
{
T* This=dynamic_ca st<T*>(this);
This->_value=t.value ;
return *This;
}

template<class T>
T& Timer::operator =(const int value)
{
T* This=dynamic_ca st<T*>(this);
This->_value=value ;
return *This;
}

class tRCD : public Timer {
public:
tRCD(const int value);
~tRCD();
};

tRCD::tRCD(cons t int value)
{
_value=value;
}

tRCD::~tRCD()
{
}

int main()
{
tRCD trcd1(1);
cout << "trcd1._val ue: " << trcd1._value << endl;
tRCD trcd2(2);
cout << "trcd2._val ue: " << trcd2._value << endl;
trcd1=trcd1+trc d2;
cout << "trcd1._val ue: " << trcd1._value << endl;
trcd1=1+trcd1;
cout << "trcd1._val ue: " << trcd1._value << endl;
trcd1=5;
cout << "trcd1._val ue: " << trcd1._value << endl;
// trcd1.operator= (trcd1.operator +(1));
trcd1=trcd1+1;
cout << "trcd1._val ue: " << trcd1._value << endl;
return 0;
}

Mar 21 '07
11 1980
On Mar 23, 8:37 am, "Zilla" <zill...@bellso uth.netwrote:
On Mar 22, 5:33 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:


Zillawrote:
On Mar 22, 4:06 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>[..]
> class D1 : public A<D1{ // derived one
> public:
> // constructors are not inherited, so you need one here
> D1(int v) : A<D1>(v) {}
> };
> class D2 : public A<D2{ // derived two
> public:
> // constructors are not inherited, so you need one here
> D2(int v) : A<D2>(v) {}
> };
>[..]
>Would that work? Use it as a starting point.
>V
Thanks, so far it worked for these test cases. It's scary how the
compiler knows how to implement the "=" operation. I'll code it so as
NOT to leave it to chance.
int main()
{
D1 d33(1);
D1 d22(1);
D1 d11=d33;
cout << "d11.value: " << d11.getVal() << endl;
d11=1;
cout << "d11.value: " << d11.getVal() << endl;
d11 = d11 + 1;
cout << "d11.value: " << d11.getVal() << endl;
d11 = 1 + d11;
cout << "d11.value: " << d11.getVal() << endl;
d11 = d22 + d11;
cout << "d11.value: " << d11.getVal() << endl;
return 0;
}
Just to bring it to your attention: it is important to not get the
types mixed up when copy-pasting your code. The class 'D1' is
defined as deriving from 'A<D1>' and 'D2' is from 'A<D2>'. This
is known as CRTP, IIRC. If when you define some 'D3' you make it
derive from 'A<D2>', the stuff can start going wrong and it might
be difficult to find. Or maybe not... Good luck!
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -
- Show quoted text -

Thanks. Yes I noticed this right off. I'm playing with this code now,
and even trying out the straight-forward use of the template, ie,
without the class declarations inheritting the template.

class D1 {
blah()

};

class D2 {
blah()

};

int main()
{
A<D1d1;
A<D2d2;
d1=blah...

}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
Ok, I was having problems with your example, so I went back to basics.
I stripped it down to the following code for operator=() only. I can't
even get it to compile...
g++ -c timers.cpp
timers.cpp: In function `int main()':
timers.cpp:35: no match for `tRCD & = double'
timers.cpp:30: candidates are: class tRCD & tRCD::operator =(const
tRCD &)

Why isn't the Timer<T>::opera tor=(...) being seen?

#include <iostream>

////////////////////////////////////////////////////////
// Base class that defines ALL the operations
template<class T>
class Timer {
public:
Timer();
T& operator=(const double v);

double _min;
};

template<class T>
Timer<T>::Timer () : _min(0)
{
}

template<class T>
T& Timer<T>::opera tor=(const double v)
{
// do something
}

class tRCD : public Timer<tRCD{
public:
tRCD() : Timer<tRCD>() {}
~tRCD() {}
};

int main()
{
tRCD t1;
t1=3.0;
cout << "t1.min: " << t1._min << endl;
}

Mar 26 '07 #11
Zilla wrote:
On Mar 23, 8:37 am, "Zilla" <zill...@bellso uth.netwrote:
>On Mar 22, 5:33 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:


>>Zillawrote:
On Mar 22, 4:06 pm, "Victor Bazarov" <v.Abaza...@com Acast.net>
wrote:
[..]
>>>> class D1 : public A<D1{ // derived one
public:
// constructors are not inherited, so you need one here
D1(int v) : A<D1>(v) {}
};
>>>> class D2 : public A<D2{ // derived two
public:
// constructors are not inherited, so you need one here
D2(int v) : A<D2>(v) {}
};
[..]
>>>>Would that work? Use it as a starting point.
>>>>V
>>>Thanks, so far it worked for these test cases. It's scary how the
compiler knows how to implement the "=" operation. I'll code it so
as NOT to leave it to chance.
>>>int main()
{
D1 d33(1);
D1 d22(1);
D1 d11=d33;
cout << "d11.value: " << d11.getVal() << endl;
d11=1;
cout << "d11.value: " << d11.getVal() << endl;
d11 = d11 + 1;
cout << "d11.value: " << d11.getVal() << endl;
d11 = 1 + d11;
cout << "d11.value: " << d11.getVal() << endl;
d11 = d22 + d11;
cout << "d11.value: " << d11.getVal() << endl;
return 0;
}
>>Just to bring it to your attention: it is important to not get the
types mixed up when copy-pasting your code. The class 'D1' is
defined as deriving from 'A<D1>' and 'D2' is from 'A<D2>'. This
is known as CRTP, IIRC. If when you define some 'D3' you make it
derive from 'A<D2>', the stuff can start going wrong and it might
be difficult to find. Or maybe not... Good luck!
>>V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide
quoted text -
>>- Show quoted text -

Thanks. Yes I noticed this right off. I'm playing with this code now,
and even trying out the straight-forward use of the template, ie,
without the class declarations inheritting the template.

class D1 {
blah()

};

class D2 {
blah()

};

int main()
{
A<D1d1;
A<D2d2;
d1=blah...

}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Ok, I was having problems with your example, so I went back to basics.
I stripped it down to the following code for operator=() only. I can't
even get it to compile...
>g++ -c timers.cpp
timers.cpp: In function `int main()':
timers.cpp:35: no match for `tRCD & = double'
timers.cpp:30: candidates are: class tRCD & tRCD::operator =(const
tRCD &)

Why isn't the Timer<T>::opera tor=(...) being seen?

#include <iostream>

////////////////////////////////////////////////////////
// Base class that defines ALL the operations
template<class T>
class Timer {
public:
Timer();
T& operator=(const double v);

double _min;
};

template<class T>
Timer<T>::Timer () : _min(0)
{
}

template<class T>
T& Timer<T>::opera tor=(const double v)
{
// do something
}

class tRCD : public Timer<tRCD{
public:
tRCD() : Timer<tRCD>() {}
~tRCD() {}
};

int main()
{
tRCD t1;
t1=3.0;
cout << "t1.min: " << t1._min << endl;
}
The assignment operator from the base class is hidden by the
assignment operator from the derived class. You need to add
a line with 'using' in it to the 'tRCD' class.

The next thing is to decide what your Timer<T>::opera tor=
is going to return. I am not sure how you're going to handle
that, since you can't really return *this (the usual value
returned from the assignment op).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 27 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2624
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
16
3101
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that 'adding' two objects will sum up the corresponding int members.
2
2070
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the overloading of operator<<. In the .cc of the respective class or in a file where I am overloading operator<< for all classes? Cheers,
8
1354
by: John Hardin | last post by:
All: The syntax for overloading the "+" and other simple binary math operators is pretty straightforward, but I can't seem to wrap my brain around the idea that overloading the "+" operator simultaneously overloads the "+=" operator. Here's the problem I'm having with it: When you overload a binary operator like "+", you accept two arguments and
67
8675
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used in low-level numerical computations, replacing Fortran in newer projects. Check, for example, sourceforge.net or freshmeat.net But neither language offers a primitive exp operator.
17
2545
by: Student | last post by:
Hi All, I have an assignment for my Programming language project to create a compiler that takes a C++ file as input and translate it into the C file. Here I have to take care of inheritance and operator overloading and virtual functions. I mean it should not hamper the C++ meaning. In frank and simple terms i need to implement a small C++ compiler in C++, and i want the intermediate representation to be C. Please help me in this....
3
2302
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
2
2265
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It uses map<and heavy overloading. The problem is, the output file differs from input, and for the love of me I can't figure out why ;p #include <iostream> #include <fstream> #include <sstream>
3
3286
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
8
2979
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
0
9718
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10614
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10109
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.