none
关于操作符重载的简单问题 RRS feed

  • 问题

  • 由于我不想用friend关键字,对于类graduate ,我的实现如下:
    //graduate.h

    #ifndef GRADUATE_H
    #define GRADUATE_H

    #include <iostream>
    #include <string>

    using namespace std;

    class graduate
    {
        public:
            /** Default constructor */
            graduate();
            string getDegree() const;
            bool isEqual(const graduate& rhs);
            bool lessThan(const graduate& rhs);

            istream& getfrom(istream& istr)const;
            ostream& sendto(ostream& ostr)const;

        protected:
        private:
        string name;
        string degree;
    };
    bool operator==(const graduate& lhs,graduate& rhs)
    {
        return lhs.isEqual(rhs);
    }
    bool operator<(const graduate& lhs,graduate& rhs)
    {
        return lhs.lessThan(rhs);

    }
    istream& operator>>(istream& istr,const graduate& grad)const
    {
        return grad.getfrom(istr);
    }
    ostream& operator<<(ostream& ostr,const graduate& grad)const
    {
        return grad.sendto(ostr);
    }
    graduate::graduate()
    {
        //ctor
    }
    /** @brief getDegree
      *
      * @todo: document this function
      */
    string graduate::getDegree() const
    {
        return degree;
    }

    /** @brief getfrom
      *
      * @todo: document this function
      */
    istream & graduate::getfrom(istream& istr)const
    {
        char ch;
        istr>>this.name;
        istr>>ch;
        istr>>this.degree;
        return istr;


    }

    /** @brief isEqual
      *
      * @todo: document this function
      */
    bool graduate::isEqual(const graduate& rhs)
    {
        return this.name==rhs.name;
    }

    /** @brief sendto
      *
      * @todo: document this function
      */
    ostream & graduate::sendto(ostream& ostr)const
    {
        ostr<<"Name:"<< this.name<<"\tdegree:"<<this.degree<<endl;
        return ostr;
    }
    /** @brief lessThan
      *
      * @todo: document this function
      */
    bool graduate::lessThan(const graduate& rhs)
    {
        return this.name<rhs.name;

    }



    #endif // GRADUATE_H
    //end of graduate.h
    编译器给出了一大堆错语:
    F:\CodeBlocksProject\listApp\graduate.h||In function `bool operator==(const graduate&, graduate&)':|
    F:\CodeBlocksProject\listApp\graduate.h|28|error: passing `const graduate' as `this' argument of `bool graduate::isEqual(const graduate&)' discards qualifiers|
    F:\CodeBlocksProject\listApp\graduate.h||In function `bool operator<(const graduate&, graduate&)':|
    F:\CodeBlocksProject\listApp\graduate.h|32|error: passing `const graduate' as `this' argument of `bool graduate::lessThan(const graduate&)' discards qualifiers|
    F:\CodeBlocksProject\listApp\graduate.h|36|error: non-member function `std::istream& operator>>(std::istream&, const graduate&)' cannot have `const' method qualifier|
    F:\CodeBlocksProject\listApp\graduate.h|40|error: non-member function `std::ostream& operator<<(std::ostream&, const graduate&)' cannot have `const' method qualifier|
    F:\CodeBlocksProject\listApp\graduate.h||In member function `std::istream& graduate::getfrom(std::istream&) const':|
    F:\CodeBlocksProject\listApp\graduate.h|63|error: request for member `name' in `this', which is of non-class type `const graduate* const'|
    F:\CodeBlocksProject\listApp\graduate.h|65|error: request for member `degree' in `this', which is of non-class type `const graduate* const'|
    F:\CodeBlocksProject\listApp\graduate.h||In member function `bool graduate::isEqual(const graduate&)':|
    F:\CodeBlocksProject\listApp\graduate.h|77|error: request for member `name' in `this', which is of non-class type `graduate* const'|
    F:\CodeBlocksProject\listApp\graduate.h||In member function `std::ostream& graduate::sendto(std::ostream&) const':|
    F:\CodeBlocksProject\listApp\graduate.h|86|error: request for member `name' in `this', which is of non-class type `const graduate* const'|
    F:\CodeBlocksProject\listApp\graduate.h|86|error: request for member `degree' in `this', which is of non-class type `const graduate* const'|
    F:\CodeBlocksProject\listApp\graduate.h||In member function `bool graduate::lessThan(const graduate&)':|
    F:\CodeBlocksProject\listApp\graduate.h|95|error: request for member `name' in `this', which is of non-class type `graduate* const'|
    ||=== Build finished: 10 errors, 0 warnings ===|

    我不知道错在哪里,请指教,非常感谢。

    Sam


    Sam
    2009年3月27日 9:53

答案

  • this是指针,所有this.的地方都改成this->
    2009年3月27日 10:48
    版主
  • 过了好几个月了,LZ应该已经解决这个问题了吧,但我还是把我的答案贴在这里吧,以期有其他人遇到这个问题也google到这里后能看到.

    bool operator==(const graduate& lhs,graduate& rhs)
    {
        return lhs.isEqual(rhs);
    }
    在这里,你的形参lhs既然是被const限定的,那么lhs的内容是不应该被修改的.编译器拒绝这样的调用:lhs.isEqual(rhs),是因为你的isEqual函数没有保证不修改lhs
    里的东西.你在isEqual的声明和定义都后面加个const声明,注意哦,不是前面哦.这样:
    bool graduate::isEqual(const graduate& rhs) const
    {
        return this.name==rhs.name;
    }
    就行了.其他这样的错误也是一个道理的.
    2009年12月11日 14:00

全部回复

  • this是指针,所有this.的地方都改成this->
    2009年3月27日 10:48
    版主
  • this是指针,所有this.的地方都改成this->
    何必要加this呢
    2009年3月28日 10:40
  • 过了好几个月了,LZ应该已经解决这个问题了吧,但我还是把我的答案贴在这里吧,以期有其他人遇到这个问题也google到这里后能看到.

    bool operator==(const graduate& lhs,graduate& rhs)
    {
        return lhs.isEqual(rhs);
    }
    在这里,你的形参lhs既然是被const限定的,那么lhs的内容是不应该被修改的.编译器拒绝这样的调用:lhs.isEqual(rhs),是因为你的isEqual函数没有保证不修改lhs
    里的东西.你在isEqual的声明和定义都后面加个const声明,注意哦,不是前面哦.这样:
    bool graduate::isEqual(const graduate& rhs) const
    {
        return this.name==rhs.name;
    }
    就行了.其他这样的错误也是一个道理的.
    2009年12月11日 14:00