473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Operator Overloading in Template Classes

Hi, I have the following code:

#include <string>
using namespace std;

template <class T>
class MyTemplate {
public:
MyTemplate(){
size = 0;
}

MyTemplate<T>& operator+=(const MyTemplate<T>& rhs){
this->val = rhs.getVal(); //*** gives me an error
this->size = rhs.getSize(); //*** gives me an error
return *this;
}

int getSize(){
return size;
}

T& getVal(){
return val;
}

void setVal(T& val){
this->val = val;
}

void setSize(int size){
this->size = size;
}
private:
T val;
int size;
};

int main()
{
MyTemplate<string> myTemp;
MyTemplate<string> myTemp2;

string myString = "Test";
myTemp2.setVal(myString);
myTemp2.setSize(myString.size());
myTemp += myTemp2;

return 0;
}
//--------------- end of sample code

This gives me the errors:
no matching function for call to 'MyTemplate<std::string>::getVal()
const'
note: candidates are: T& MyTemplate<T>::getVal() [with T = std::string]
<near match>
error: no matching function for call to
'MyTemplate<std::string>::getSize() const'
note: candidates are: int MyTemplate<T>::getSize() [with T =
std::string] <near match>

Is this because the functions getVal() and getSize() are defined in the
same file as they are being invoked? How can I get around this problem?
Thanks

Sep 9 '05 #1
3 1624
jo***********@gmail.com wrote:
Hi, I have the following code:

#include <string>
using namespace std;

template <class T>
class MyTemplate {
public:
MyTemplate(){
size = 0;
}

MyTemplate<T>& operator+=(const MyTemplate<T>& rhs){
this->val = rhs.getVal(); //*** gives me an error
this->size = rhs.getSize(); //*** gives me an error
return *this;
}

int getSize(){
int getSize() const {

return size;
}

T& getVal(){
return val;
}
// Need this one, too (or instead of the previous)
T getVal() const {
return val;
}

[snip] This gives me the errors:
no matching function for call to 'MyTemplate<std::string>::getVal()
const'
note: candidates are: T& MyTemplate<T>::getVal() [with T = std::string]
<near match>
error: no matching function for call to
'MyTemplate<std::string>::getSize() const'
note: candidates are: int MyTemplate<T>::getSize() [with T =
std::string] <near match>

Is this because the functions getVal() and getSize() are defined in the
same file as they are being invoked? How can I get around this problem?


No. It's because the operator+= has a const parameter (rhs), and no
const methods are available. See these FAQs:

http://www.parashift.com/c++-faq-lit...rrectness.html

Cheers! --M

Sep 9 '05 #2
jo***********@gmail.com wrote:
MyTemplate<T>& operator+=(const MyTemplate<T>& rhs){
this->val = rhs.getVal(); //*** gives me an error
this->size = rhs.getSize(); //*** gives me an error
return *this;
}

This has nothing to do with it being a template. getVal and getSize
are not declared as const methods and hence you can't call them on
rhs (which is const).
Sep 9 '05 #3
mlimber wrote:
jo***********@gmail.com wrote:
Hi, I have the following code:

#include <string>
using namespace std;

template <class T>
class MyTemplate {
public:
MyTemplate(){
size = 0;
}

MyTemplate<T>& operator+=(const MyTemplate<T>& rhs){
this->val = rhs.getVal(); //*** gives me an error
this->size = rhs.getSize(); //*** gives me an error
return *this;
}

int getSize(){


int getSize() const {

return size;
}

T& getVal(){
return val;
}


// Need this one, too (or instead of the previous)
T getVal() const {
return val;
}

[snip]
This gives me the errors:
no matching function for call to 'MyTemplate<std::string>::getVal()
const'
note: candidates are: T& MyTemplate<T>::getVal() [with T = std::string]
<near match>
error: no matching function for call to
'MyTemplate<std::string>::getSize() const'
note: candidates are: int MyTemplate<T>::getSize() [with T =
std::string] <near match>

Is this because the functions getVal() and getSize() are defined in the
same file as they are being invoked? How can I get around this problem?


No. It's because the operator+= has a const parameter (rhs), and no
const methods are available. See these FAQs:

http://www.parashift.com/c++-faq-lit...rrectness.html

Cheers! --M


Wonderful, Thanks!! One more thing is that your const member functions
returning references should return const values too or the compiler
will complain (well, at least GCC did). So:

const T& getVal() const{
return val;
}

Sep 9 '05 #4

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

Similar topics

3
by: CoolPint | last post by:
After upgrading to gcc 3.4.2 from gcc 3.2.3, I got compiler errors that I could not figure out. After reading other postings, I learned that my coding was not compliant to the standard in the first...
2
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...
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
4
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...
5
by: Fabio Fracassi | last post by:
Hi, I belive what i am trying to do is not possible, but I hope someone can change my mind. Here is some code i'd like to write: template <class type> class engine1 {}; template <class...
3
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based...
2
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class...
11
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...
1
by: amhoov | last post by:
Hi all, OS - MacOS X 10.4.9 Compiler - gcc 4.0 (XCode) I'm *extremely* new to C++ (but have extensive experience with Java), so please go easy on me. I'm running into a very odd situation...
14
by: Hunk | last post by:
Hi I ws wondering if there is a way to implement operator+ in case of virtual classes. Here's the problem. I have to have a base string class from which two classes (normal char string and a...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.