[英]Overloaded non-member operator== undefined?
g ++ 4.5.3(cygwin)
我在定义重载的非成员运算符==时遇到了麻烦。 编译器输出错误消息
main.cpp:11:对`slip :: operator ==(bool,slip :: SlipDatum const&)的未定义引用
而且我不知道我在做什么错。 在我看来,所有定义都是可见的,所有函数原型和代码都是正确的。 将重载用作成员函数时,如“(SlipDatum&)Y ==(bool)X”,则没有问题。 作为非成员函数,我不断获得未定义的引用。
另一个问题是,当从Slip.cpp中删除“ #include SlipOp.h”时,在Slip.cpp中未定义SlipDatum。我查看了SlipDatum.h,不需要SlipOp.h。我查看了SlipDatum .cpp和SlipOp.h是必需的,包括在内,而SlipOp.h不需要或在Slip.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
#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
# 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
#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
#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
#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
# include "SlipDatum.h"
# include "SlipOP.h"
# include "SlipCellBase.h"
bool SlipDatum::operator==(const bool X)
{ return ((SlipOp*)*getOperator())->equal(*this, X); }
滑动速度
#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
#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
# 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
在slip.cpp中,您在全局范围内定义了operator ==,即您错过了名称空间:
namespace slip {
bool operator==(bool X, const SlipDatum& Y)
{ return const_cast<SlipDatum&>(Y) == X; };
}
PS:const_cast不必要,因为operator==(SlipDatum&, bool)
应通过const引用获取SlipDatum。 即在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.