אבי נימני8
New member
c++ בעיה עם overloading operator
מישהו יכול להסביר לי בבקשה למה הקוד שלי לא מתקמפל כאשר const מופיע במתודה isEqual אבל כן מתקמפל כאשר אני מוריד את const ?
template<class T>
class Test
{
private:
T* ptr;
public:
Test(T* ptr) : ptr(ptr)
{ }
T operator *()
{
return(*ptr);
}
};
main:
bool isEqual(const string* str, const Test<string>& test)
{
return(*str==*test); // when I use const, the error: no operator "*" matches these operands operand types are: * const Test <std::string>
}
int main()
{
string *s=new string("abcd");
string *e=new string("abcd");
Test<string> t(s);
if(isEqual(e,t)==true)
cout<<"Good";
else
cout<<"bad";
getchar();
return(0);
}
אשמח לקבל עזרה ולמה זה קורה.
תודה רבה
מישהו יכול להסביר לי בבקשה למה הקוד שלי לא מתקמפל כאשר const מופיע במתודה isEqual אבל כן מתקמפל כאשר אני מוריד את const ?
template<class T>
class Test
{
private:
T* ptr;
public:
Test(T* ptr) : ptr(ptr)
{ }
T operator *()
{
return(*ptr);
}
};
main:
bool isEqual(const string* str, const Test<string>& test)
{
return(*str==*test); // when I use const, the error: no operator "*" matches these operands operand types are: * const Test <std::string>
}
int main()
{
string *s=new string("abcd");
string *e=new string("abcd");
Test<string> t(s);
if(isEqual(e,t)==true)
cout<<"Good";
else
cout<<"bad";
getchar();
return(0);
}
אשמח לקבל עזרה ולמה זה קורה.
תודה רבה