【问题标题】:C++ overloading operator, compiler doesn't see the operatorC++重载运算符,编译器看不到运算符
【发布时间】:2018-12-20 01:52:14
【问题描述】:

CMagazin.h

class CMagazin
{
    char *m_nume;
    list<CProdus*>List_produse;
public:
    void printExpirabile( const char* data);
    ~CMagazin();
};

CMagazin.cpp

void CMagazin::printExpirabile(const char *xdata)
{
    list<CProdus*>::iterator it;
    for (it = List_produse.begin(); it != List_produse.end(); ++it)
    {
         CProdus* p = *it;
        if (p->get_tip()=='A')
        {
            **if (p > xdata)**->this problem

        }
    }
}

Caliment.h

class CAliment :
    public CProdus
{
    char *m_expirare;
public:
    bool operator >(const char*date);
    ~CAliment();    
};

Caliment.cpp

bool CAliment::operator>(const char * date)
{
    if (atoi(this->m_expirare) < atoi(date))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

关于 ">" operator.in CMagazin.cpp 不要使用我的运算符...我需要帮助。 我能做什么?我需要在 CMagazin 类中使用“>”。 CAliment 类是从 CProdus 派生的类。

【问题讨论】:

    标签: operator-overloading overloading


    【解决方案1】:

    答案是:在 CProdus 类中,操作符必须声明为虚拟的,而在 CMagazin.cpp

            void CMagazin::printExpirabile(const char *xdata)
        {
            list<CProdus*>::iterator it;
            for (it = List_produse.begin(); it != List_produse.end(); ++it)
            {
                 CProdus* p = *it;
                if (p->get_tip()=='A')
                {
                    if (p->operator>( xdata))-> make this!
                         {
                           p->print();
                         }
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      CProdus*p - 是一个指针,需要有对象才能使用这个操作符 -> Try (*p)>xdata

        void CMagazin::printExpirabile(const char *xdata)
      {
          list<CProdus*>::iterator it;
          for (it = List_produse.begin(); it != List_produse.end(); ++it)
          {
               CProdus* p = *it;
              if (p->get_tip()=='A')
              {
                  if ((*p)>( xdata))-> make this!
                       {
                         p->print();
                       }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-09
        • 1970-01-01
        • 2012-11-26
        • 1970-01-01
        • 1970-01-01
        • 2016-02-19
        • 1970-01-01
        相关资源
        最近更新 更多