我在类中声明bool operator ()(const String &s1, const String &s2)
对话框报错:
this function's declaration will not match its definition:
Declaration:"operator()(const String& s1, const String& s2)"
Definition:"operator()(const String& s1, const String& s2)"

解决方案 »

  1.   

    C++标准规定,对于二元运算符的重载,第一个参数必须是自己
    class CA
    {
    public:
    CA()
    {};
    CA(int a)
    :m_a(a){};
    bool operator==(int b)//==是二元运算符,第一个参数是自己:即CA,(默认的)
                                   //,第二个参数int b
    {
    return m_a==b;
    }
    int m_a;
    };使用例子:
    TRACE("%d\n",CA(1)==2);用这种方式重载时,操作符左右两参数是不能,交换的,
    如:TRACE("%d\n",2==CA(2));//这种写法编译都通不过
    要使第二种情况也能工作,就必须重新重载。
    class CA
    {
    public:
    CA()
    {};
    CA(int a)
    :m_a(a){};
    friend bool operator==(int b,CA& a)  //1
    {
    return a.m_a==b;
    };
    bool operator==(int b)//==是二元运算符,第一个参数是自己:即CA,(默认的)
                                   //,第二个参数int b //2
    {
    return m_a==b;
    }
    int m_a;
    } ;
    楼主比较一下//1和//2
    当然你可以把//2 定义为
    friend bool operator==(CA & a ,int b)//==是二元运算符,第一个参数是自己:即CA,(默认的)
                                   //,第二个参数int b 
    {
             return a.m_a==b;
    }
    我在类中声明bool operator ()(const String &s1, const String &s2)
    对于你这种情况,()其实是有三个参数,第一个是默认的,后两个是s1,s2
    改为friend bool operator ()(const String &s1, const String &s2)就可以了。
    罗嗦了不少,不知楼主能理解吗????????