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

overriding operator== error

P: n/a
Hi,
I have this piece code,

struct TriStr {
MyString str1;
MyString str2;
MyString str3;

TriStr(MyString s1, MyString s2, MyString s3){
this->str1 = s1;
this->str2 = s2;
this->str3 = s3;
}

TriStr& operator=(const TriStr& rhs){
this->str1 = rhs.str1;
this->str2 = rhs.str2;
this->str3 = rhs.str3;
return *this;
}

bool operator==(const TriStr& rhs){
return (this->str1.IEquals(rhs.str1)
&& (this->str2.IEquals(rhs.str2))
&& (this->str3.IEquals(rhs.str3)));
}
};

bool operator==(TriStr& rhs){
return (this->str1.IEquals(rhs.str1)
&& (this->str2.IEquals(rhs.str2))
&& (this->str3.IEquals(rhs.str3)));
}

I compile it using VC7 compiler on Windows, I get a lot of error
message, please see the messages at the end of this post.

I then find that if I add "const" to operator== argument like this,

bool operator==(const TriStr& rhs){
return (this->str1.IEquals(rhs.str1)
&& (this->str2.IEquals(rhs.str2))
&& (this->str3.IEquals(rhs.str3)));
}

Then the compiler is happy.

But I am wondering why is that?


Compile error:

c:\STL\export\stl\_algobase.c(106) : error C2678: binary '==' : no
operator found which takes a left-hand operand of
type '_STL::vector<_Tp>::value_type' (or there is no acceptable
conversion)
with
[
_Tp=Test::TriStr
]
c:\STL\export\stl\_algobase.c(203) : see reference to function
template instantiation '_RandomAccessIter _STL
::__find<_InputIter,_Tp>(_RandomAccessIter,_Random AccessIter,const _Tp
&,const _STL::random_access_iterator_tag &)' being compiled
with
[
_RandomAccessIter=_STL::vector<Test::TriStr>::iter ator,
_InputIter=_STL::vector<Test::TriStr>::iterator,
_Tp=Test::TriStr
]
../s\test.c(72) : see reference to function template
instantiation '_InputIter _STL::find<_STL::vector<_Tp>::iterator,MdtA
ddrGen::TriStr>(_InputIter,_InputIter,const _Tp &)' being compiled
with
[
_InputIter=_STL::vector<Test::TriStr>::iterator,
_Tp=Test::TriStr
]
c:\STL\export\stl\_algobase.c(109) : error C2678: binary '==' : no
operator found which takes a left-hand operand of
type '_STL::vector<_Tp>::value_type' (or there is no acceptable
conversion)
with
[
_Tp=Test::TriStr
]
c:\STL\export\stl\_algobase.c(112) : error C2678: binary '==' : no
operator found which takes a left-hand operand of
type '_STL::vector<_Tp>::value_type' (or there is no acceptable
conversion)
with
[
_Tp=Test::TriStr
]
c:\STL\export\stl\_algobase.c(115) : error C2678: binary '==' : no
operator found which takes a left-hand operand of
type '_STL::vector<_Tp>::value_type' (or there is no acceptable
conversion)
with
[
_Tp=Test::TriStr
]
c:\STL\export\stl\_algobase.c(121) : error C2678: binary '==' : no
operator found which takes a left-hand operand of
type '_STL::vector<_Tp>::value_type' (or there is no acceptable
conversion)
with
[
_Tp=Test::TriStr
]
c:\STL\export\stl\_algobase.c(124) : error C2678: binary '==' : no
operator found which takes a left-hand operand of
type '_STL::vector<_Tp>::value_type' (or there is no acceptable
conversion)
with
[
_Tp=Test::TriStr
]
c:\STL\export\stl\_algobase.c(127) : error C2678: binary '==' : no
operator found which takes a left-hand operand of
type '_STL::vector<_Tp>::value_type' (or there is no acceptable
conversion)
with
[
_Tp=Test::TriStr
]

Jun 14 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
li*****@hotmail.com wrote:
I have this piece code,

struct TriStr {
MyString str1;
MyString str2;
MyString str3;

TriStr(MyString s1, MyString s2, MyString s3){
this->str1 = s1;
this->str2 = s2;
this->str3 = s3;
}

TriStr& operator=(const TriStr& rhs){
this->str1 = rhs.str1;
this->str2 = rhs.str2;
this->str3 = rhs.str3;
return *this;
}

bool operator==(const TriStr& rhs){
This should probably read

bool operator==(const TriStr& rsh) const {
return (this->str1.IEquals(rhs.str1)
&& (this->str2.IEquals(rhs.str2))
&& (this->str3.IEquals(rhs.str3)));
}
};

bool operator==(TriStr& rhs){
return (this->str1.IEquals(rhs.str1)
&& (this->str2.IEquals(rhs.str2))
&& (this->str3.IEquals(rhs.str3)));
}

I compile it using VC7 compiler on Windows, I get a lot of error
message, please see the messages at the end of this post.

I then find that if I add "const" to operator== argument like this,
And where exactly did you add 'const'? Don't you already have two
operator== members in the class?

bool operator==(const TriStr& rhs){
return (this->str1.IEquals(rhs.str1)
&& (this->str2.IEquals(rhs.str2))
&& (this->str3.IEquals(rhs.str3)));
}

Then the compiler is happy.

But I am wondering why is that?
Probably because the compiler is trying to compare a non-const object
of your type to a const one.
[...]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 14 '06 #2

P: n/a
li*****@hotmail.com wrote:
I have this piece code,

struct TriStr { [snip] };

bool operator==(TriStr& rhs){
return (this->str1.IEquals(rhs.str1)
&& (this->str2.IEquals(rhs.str2))
&& (this->str3.IEquals(rhs.str3)));
}
If you use operator== outside a class then it must take two
arguments.
I then find that if I add "const" to operator== argument like this,

bool operator==(const TriStr& rhs){
return (this->str1.IEquals(rhs.str1)
&& (this->str2.IEquals(rhs.str2))
&& (this->str3.IEquals(rhs.str3)));
}


There must be something you aren't telling us. Post some
code that actually gives the errors that you are reporting.

Jun 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.