Operator Overloading in Template Classes

J

jois.de.vivre

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
 
M

mlimber

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-lite/const-correctness.html

Cheers! --M
 
R

Ron Natalie

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

jois.de.vivre

mlimber said:
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-lite/const-correctness.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;
}
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top