By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,312 Members | 1,877 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,312 IT Pros & Developers. It's quick & easy.

Operator Overloading in Template Classes

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.