Discussion Locked

VC version upgrade question

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

compiler error c2678 - saying Any binary operator( <<, >>, ==, !=)as not defined even though they are present. Probable reason is Iam using VC 6 whereas the code is from earlier version of VC. MSDN help describes it as a namespace problem and suggests some workarounds. Heres the code snippet.

In someclass.h:

template , class Alloc = std::allocator >
class Someclass
{
// class definition here
friend bool operator==(const
someclass &rs1,const someclass &rs2)
{}
}

In main.cpp:
struct aclass
{
aclass(char *r) {
if (r != NULL) {
std::istringstream str(r);
str >> range; }
}
someclass range;
friend operator==(const aclass &p1, const aclass &p2)
{
return 1;
}
}

6 replies

by c-wundram

You usally define an a binary operator where the first, or both operends are going to be a class object as a const class member. Plus you don't specify template parameters when referencing a class inside its defination unless you are refering to a different instantiation. Also you refer to your class as both someclass and Someclass.

Here is an examle of defining an operator== for a template class that builds in vc6

class someclass
{
public:
someclass(T t)
{
m_t = t;
}

bool operator ==(const someclass &rs2) const
{
printf("== operator called\n");
return (m_t == rs2.m_t);
}

private:

T m_t;

};

int main (int argc, char **argv)
{
someclass test1(1);
someclass test2(1);

if (test1 == test2)
printf("true\n");
else
printf("false\n");

return 0;
}

This discussion is closed to new replies.