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
#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