Note that there are some explanatory texts on larger screens.

plurals
  1. POOverloaded non-member operator== undefined?
    primarykey
    data
    text
    <p>g++ 4.5.3 (cygwin)</p> <p>I am having trouble in defining the overloaded non-member operator==. The compiler outputs the error message</p> <blockquote> <p>main.cpp:11: undefined reference to `slip::operator==(bool, slip::SlipDatum const&amp;)</p> </blockquote> <p>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&amp;)Y == (bool)X", there are no problems. As a non-member function I keep getting an undefined reference. </p> <p>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?</p> <p>The sample code used follows:</p> <p>main.cpp</p> <pre><code># include &lt;cstdlib&gt; # include "Slip.h" # include &lt;iostream&gt; using namespace std; using namespace slip; int main(int argc, char** argv) { SlipDatum Y; bool X = true; if (X == Y) cout &lt;&lt; "here" &lt;&lt; endl; return 0; } </code></pre> <p>Slip.h</p> <pre><code>#ifndef SLIP_H #define SLIP_H # include "SlipDatum.h" namespace slip { bool operator==(const bool X, const SlipDatum&amp; Y); // Y == X }; // namespace slip #endif /* SLIP_H */ </code></pre> <p>Slip.cpp</p> <pre><code># include "Slip.h" # include "SlipCellBase.h" # include "SlipOP.h" # include "SlipDatum.h" inline bool operator==(const bool X, const SlipDatum&amp; Y) { return const_cast&lt;SlipDatum&amp;&gt;(Y) == X; }; </code></pre> <p>SlipDef.h</p> <pre><code>#ifndef SLIPDEF_H #define SLIPDEF_H # include &lt;string&gt; 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 */ </code></pre> <p>SlipCellBase.h</p> <pre><code>#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 &amp;const_cast&lt;SlipCellBase*&gt;(this)-&gt;operation; } static void** getOperator(SlipCellBase* X) { return &amp;(X-&gt;operation); } }; // class SlipCellBase }; // namespace slip #endif /* SLILPCELLBASE_H */ </code></pre> <p>SlipDatum.h</p> <pre><code>#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 </code></pre> <p>SlipDatum.cpp</p> <pre><code># include "SlipDatum.h" # include "SlipOP.h" # include "SlipCellBase.h" bool SlipDatum::operator==(const bool X) { return ((SlipOp*)*getOperator())-&gt;equal(*this, X); } </code></pre> <p>SlipOp.h</p> <pre><code>#ifndef _SLIPOP_H #define _SLIPOP_H # include "SlipDatum.h" using namespace slip; namespace slip { class SlipOp { public: virtual bool equal(SlipDatum&amp; Y, const bool X) = 0; }; // class SlipOp }; // namespace slip #endif /* _SLIPOP_H */ </code></pre> <p>SlipBool.h</p> <pre><code>#ifndef _SLIPboolOP_H #define _SLIPboolOP_H # include "SlipOp.h" namespace slip { class SlipBoolOp : public SlipOp { public: virtual bool equal(SlipDatum&amp; Y, const bool X); }; // class SlipBoolOp }; // namespace slip #endif /* SLIPboolOP_H */ </code></pre> <p>SlipBool.cpp</p> <pre><code># include "SlipBoolOp.h" # include "SlipDatum.h" using namespace slip; namespace slip { bool SlipBoolOp::equal (SlipDatum&amp; Y, const bool X) { return false; }; // bool SlipBoolOp::equal (SlipDatum&amp; Y, const SlipDatum&amp; X) }; // namespace slip </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload