Note that there are some explanatory texts on larger screens.

plurals
  1. POoperators on Iterators in C++
    primarykey
    data
    text
    <p>I am porting an old code base to OSX.</p> <p>I have the following snippet of code:</p> <pre><code>FxLayerList::iterator lastVisible = NULL; for (FxLayerList::iterator iter = mBranch.begin(); iter != mBranch.end(); iter++) { if ( (*iter)-&gt;IsVisible() &amp;&amp; !(*iter)-&gt;IsBypass()) { lastVisible = iter; } } if (lastVisible != NULL &amp;&amp; (*lastVisible)-&gt;GetGeneratedImage()) { </code></pre> <p>I get an error that says: <code>error: no match for 'operator!=' in 'lastVisible != 0'</code></p> <p>I dont follow, I thought operations like != and ==, etc were standard operations. Why the complaint from the compiler?</p> <p>UPDATE: I am trying to understand the comparison of objects. What if the code is like this:</p> <pre><code>FxBool FxLayerList::Contains(FxLayer *layer) const { for (FxLayerList::const_iterator iter=this-&gt;begin(); iter != this-&gt;end(); iter++) { if ((*iter) == layer) { return true; } } return false; } </code></pre> <p>with errors like: <code>error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:</code></p> <p>What is the core concept I am missing?</p> <p>Update 2: </p> <pre><code>// FxSmartPtr is a smart pointer that is also typed for each class, avoiding the need for any casting. // Setting an FxSmartPtr to NULL actually kills the memory that it's pointing to as well. template &lt;class eachClass&gt; class FxSmartPtr { public: // Construction // Default constructor makes an empty container. FxSmartPtr(void) : mPtr(NULL) {} // Construction with a ptr adds one reference to it. FxSmartPtr(eachClass *ptr) : mPtr(ptr) { this-&gt;Reference(); } // Copy construction means that both smart pointers end up with a reference to the object. FxSmartPtr(const FxSmartPtr &amp; inPtr) :mPtr(NULL) { FrAssignRef(mPtr,(eachClass *)inPtr.mPtr); } // Default construction FxSmartPtr(FxConstructArg cons) { if (cons == FcNew) mPtr = new eachClass(); } FxSmartPtr(FxConstructArg cons,eachClass *ptr) { if (cons == FcNew) mPtr = ptr; } // Destructor removes only the one reference that we own. ~FxSmartPtr() { this-&gt;Dispose(); } // Most important and common use is via assignment. References are always safely balanced. // AssignReference safely replaces one reference counted ptr with another. static inline eachClass * FrAssignRef(eachClass *&amp; to, eachClass * from) { if (from) from-&gt;AddReference(); if (to) to-&gt;RemoveReference(); to = from; return to; } // If you assign a pointer to this object we add one reference count to it. const FxSmartPtr&lt;eachClass&gt; &amp; operator = (const eachClass *ptr) { FrAssignRef(mPtr,(eachClass *)ptr); return *this; } // Replace our referenced object with a reference added to the incoming one. const FxSmartPtr&lt;eachClass&gt; &amp; operator = (const FxSmartPtr &amp; inPtr) { FrAssignRef(mPtr,(eachClass *)inPtr.mPtr); return *this; } // Assignment to a dumb pointer takes/gives no references. operator eachClass * (void) const { return mPtr; } eachClass * operator-&gt;(void) { if (mPtr != NULL) if (mPtr-&gt;GetRefCount() &lt; 1 || mPtr-&gt;GetRefCount() &gt; 10000) ASSERT(0); return mPtr; } const eachClass * operator-&gt;(void) const { if (mPtr != NULL) if (mPtr-&gt;GetRefCount() &lt; 1 || mPtr-&gt;GetRefCount() &gt; 10000) ASSERT(0); return mPtr; } // Explicit assignment and object transfers // Get() - return ptr with no reference eachClass * Get(void) const { return mPtr; } eachClass * GetPtr(void) { return mPtr; } // Own() - return ownership with ptr eachClass * Own(void) { if (mPtr) mPtr-&gt;AddReference(); return mPtr; } // Set() - we take our own reference on your object FxSmartPtr&lt;eachClass&gt; &amp; Set(eachClass * ptr) { FrAssignRef(mPtr, ptr); return *this; } // Take() - you give us your reference FxSmartPtr&lt;eachClass&gt; &amp; Take(eachClass * ptr) { FrDispose(mPtr); mPtr = ptr; return *this; } // Comparison operators compare the pointers contained in each FxBool operator == (const FxSmartPtr &amp; inPtr) const { return (mPtr == inPtr.mPtr); } FxBool operator == (const eachClass * inPtr) const { return (mPtr == inPtr); } FxBool operator != (const FxSmartPtr &amp; inPtr) const { return (mPtr != inPtr.mPtr); } FxBool operator != (const eachClass * inPtr) const { return (mPtr != inPtr); } // Reference() and Dispose() change the normal reference count. If you use these then // you end up having to count references externally. // Safely take a reference if the ptr is not nil void Reference(void) { if (mPtr != NULL) mPtr-&gt;AddReference(); } // Safely dispose one reference count. void Dispose(void) { if (mPtr != NULL) // JASON/INDIE - SLACKMOEHRLE@GMAIL.COM // { ULONG refs = mPtr-&gt;GetRefCount(); mPtr-&gt;RemoveReference(); if (refs &lt;= 1) mPtr = NULL; } } { FxUInt32 refs = mPtr-&gt;GetRefCount(); mPtr-&gt;RemoveReference(); if (refs &lt;= 1) mPtr = NULL; } } protected: eachClass *mPtr; }; </code></pre>
    singulars
    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.
 

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