简体   繁体   English

重载的非成员运算符==未定义?

[英]Overloaded non-member operator== undefined?

g++ 4.5.3 (cygwin) g ++ 4.5.3(cygwin)

I am having trouble in defining the overloaded non-member operator==. 我在定义重载的非成员运算符==时遇到了麻烦。 The compiler outputs the error message 编译器输出错误消息

main.cpp:11: undefined reference to `slip::operator==(bool, slip::SlipDatum const&) main.cpp:11:对`slip :: operator ==(bool,slip :: SlipDatum const&)的未定义引用

and I can't figure out what I'm doing wrong. 而且我不知道我在做什么错。 It seems to me that all of the definitions are visible and all the function prototypes and code are correct. 在我看来,所有定义都是可见的,所有函数原型和代码都是正确的。 When the overload is used as a member function, as in "(SlipDatum&)Y == (bool)X", there are no problems. 将重载用作成员函数时,如“(SlipDatum&)Y ==(bool)X”,则没有问题。 As a non-member function I keep getting an undefined reference. 作为非成员函数,我不断获得未定义的引用。

Another issue is that when '#include SlipOp.h"' is removed from Slip.cpp then SlipDatum is undefined in Slip.cpp. I've looked at SlipDatum.h and SlipOp.h is not needed. I've looked at SlipDatum.cpp and SlipOp.h is needed and included. SlipOp.h is not needed or referenced in Slip.cpp, so why does the compiler think that it's needed? 另一个问题是,当从Slip.cpp中删除“ #include SlipOp.h”时,在Slip.cpp中未定义SlipDatum。我查看了SlipDatum.h,不需要SlipOp.h。我查看了SlipDatum .cpp和SlipOp.h是必需的,包括在内,而SlipOp.h不需要或在Slip.cpp中引用,那么为什么编译器会认为它是必需的?

The sample code used follows: 使用的示例代码如下:

main.cpp main.cpp

# include <cstdlib>
# include "Slip.h"
# include <iostream>
using namespace std;
using namespace slip;

int main(int argc, char** argv) {
   SlipDatum Y;
   bool X = true;
   if (X == Y) cout << "here" << endl;
   return 0;
}

Slip.h Slip.h

#ifndef SLIP_H
#define SLIP_H
# include "SlipDatum.h"

namespace slip {
   bool      operator==(const bool   X, const SlipDatum& Y);  // Y == X
}; // namespace slip
#endif  /* SLIP_H */

Slip.cpp Slip.cpp

# include "Slip.h"
# include "SlipCellBase.h"
# include "SlipOP.h"
# include "SlipDatum.h"

inline  bool operator==(const bool X, const SlipDatum& Y) 
     { return const_cast<SlipDatum&>(Y) == X; };

SlipDef.h SlipDef.h

#ifndef SLIPDEF_H
#define SLIPDEF_H

# include <string>

using namespace std;

namespace slip {

#ifdef UCHAR
   # undef UCHAR
#endif
#ifdef ULONG
   # undef ULONG
#endif
#ifdef DOUBLE
   # undef DOUBLE
#endif
#ifdef PTR
   # undef PTR
#endif

   typedef unsigned char   UCHAR;               //  8-bits
   typedef unsigned long   ULONG;               // 32-bits
   typedef double          DOUBLE;              // 64-bits
   typedef void *          PTR;                 // pointer
   typedef string *        STRING;              // C String

   union Data {                                 // Slip data contents
       bool      Bool;                          // compiler defined
       char      Chr;                           //  8-bits
       UCHAR     UChr;                          //  8-bits
       long      Long;                          // 32-bits
       ULONG     ULong;                         // 32-bits
       double    Double;                        // 64-bits
       PTR       Ptr;                           // 
       STRING    String;                        // pointer to a string
   }; // union Data
} // namespace slip
#endif  /* SLIPDEF_H */

SlipCellBase.h SlipCellBase.h

#ifndef _SLIPCELLBASE_H
#define _SLIPCELLBASE_H

# include "SlipDef.h"

using namespace std;

namespace slip {
  class SlipCellBase {
     friend class SlipOp;
  private:
      void*         operation;   //! Operations cell can perform
      SlipCellBase* leftLink;    //! Pointer to preceding cell
      SlipCellBase* rightLink;   //! Pointer to following cell
      Data          datum;       //! SLIP cell data field
 protected:
      void** getOperator() const 
           { return &const_cast<SlipCellBase*>(this)->operation; }
      static void** getOperator(SlipCellBase* X) 
           { return   &(X->operation);   }
  }; // class SlipCellBase
}; // namespace slip
#endif  /* SLILPCELLBASE_H */

SlipDatum.h SlipDatum.h

#ifndef SLIP_DATUM_H
#define SLIP_DATUM_H
# include "SlipCellBase.h"
namespace slip {
  class SlipDatum : public SlipCellBase {
  public:
     bool       operator==(const bool X);  // Y == X
  }; // SlipDatum
}; // namespace slip
#endif

SlipDatum.cpp SlipDatum.cpp

# include "SlipDatum.h"
# include "SlipOP.h"
# include "SlipCellBase.h"

bool SlipDatum::operator==(const bool X)
    { return ((SlipOp*)*getOperator())->equal(*this, X); }

SlipOp.h 滑动速度

#ifndef _SLIPOP_H
#define _SLIPOP_H
# include "SlipDatum.h"

using namespace slip;

namespace slip {
  class SlipOp {
  public:
     virtual bool equal(SlipDatum& Y, const bool X)  = 0;
  }; // class SlipOp
}; // namespace slip
#endif  /* _SLIPOP_H */

SlipBool.h SlipBool.h

#ifndef _SLIPboolOP_H
#define _SLIPboolOP_H
# include "SlipOp.h"

namespace slip {
  class SlipBoolOp : public SlipOp {
  public:
     virtual bool equal(SlipDatum& Y, const bool   X);
  }; // class SlipBoolOp
}; // namespace slip
#endif  /* SLIPboolOP_H */

SlipBool.cpp SlipBool.cpp

# include "SlipBoolOp.h"
# include "SlipDatum.h"

using namespace slip;

namespace slip {
  bool SlipBoolOp::equal (SlipDatum& Y, const bool X) {
    return false;
  }; // bool SlipBoolOp::equal (SlipDatum& Y, const SlipDatum& X) 

}; // namespace slip

in slip.cpp you define the operator== in global scope, ie you missed the namespace: 在slip.cpp中,您在全局范围内定义了operator ==,即您错过了名称空间:

namespace slip {
  bool operator==(bool X, const SlipDatum& Y) 
  { return const_cast<SlipDatum&>(Y) == X; };
}

PS: the const_cast should not be necessary, since operator==(SlipDatum&, bool) should take the SlipDatum by const reference as well. PS:const_cast不必要,因为operator==(SlipDatum&, bool)应通过const引用获取SlipDatum。 ie in slipdatum.h/cpp: 即在slipdatum.h / cpp中:

namespace slip {
  class SlipDatum : public SlipCellBase {
  public:
     bool       operator==(bool X) const; // <--! *this is left constant                                          
  }; 
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM