overriding operator== error

L

linq936

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,_RandomAccessIter,const _Tp
&,const _STL::random_access_iterator_tag &)' being compiled
with
[
_RandomAccessIter=_STL::vector<Test::TriStr>::iterator,
_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
]
 
V

Victor Bazarov

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
 
O

Old Wolf

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.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top