2011-05-18 46 views
0

我正在將一箇舊的代碼移植到OSX中。運算符在C++中的迭代器中

我有下面的代碼片段:

FxLayerList::iterator lastVisible = NULL; 
for (FxLayerList::iterator iter = mBranch.begin(); iter != mBranch.end(); iter++) { 
    if ((*iter)->IsVisible() && !(*iter)->IsBypass()) { 
     lastVisible = iter; 
    } 
} 
if (lastVisible != NULL && (*lastVisible)->GetGeneratedImage()) { 

我得到一個錯誤,指出:error: no match for 'operator!=' in 'lastVisible != 0'

我不跟着,我以爲操作,如=和==等都是標準的操作!爲什麼來自編譯器的投訴?

更新:我想了解對象的比較。如果代碼是什麼這樣的:

FxBool FxLayerList::Contains(FxLayer *layer) const 
{ 
for (FxLayerList::const_iterator iter=this->begin(); iter != this->end(); iter++) 
{ 
    if ((*iter) == layer) { 
     return true; 
    } 
} 
return false; 
} 

與類似的錯誤: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:

核心是什麼概念,我很想念?

更新2:

// 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 <class eachClass> 
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->Reference(); } 

// Copy construction means that both smart pointers end up with a reference to the object. 
FxSmartPtr(const FxSmartPtr & 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->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 *& to, eachClass * from) 
    { if (from) from->AddReference(); if (to) to->RemoveReference(); to = from; return to; } 

// If you assign a pointer to this object we add one reference count to it. 
const FxSmartPtr<eachClass> & operator = (const eachClass *ptr) 
    { FrAssignRef(mPtr,(eachClass *)ptr); return *this; } 

// Replace our referenced object with a reference added to the incoming one. 
const FxSmartPtr<eachClass> & operator = (const FxSmartPtr & 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->(void) 
    { if (mPtr != NULL) if (mPtr->GetRefCount() < 1 || mPtr->GetRefCount() > 10000) ASSERT(0); return mPtr; } 
const eachClass * operator->(void) const 
    { if (mPtr != NULL) if (mPtr->GetRefCount() < 1 || mPtr->GetRefCount() > 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->AddReference(); return mPtr; } 

// Set() - we take our own reference on your object 
FxSmartPtr<eachClass> & Set(eachClass * ptr) 
    { FrAssignRef(mPtr, ptr); return *this; } 

// Take() - you give us your reference 
FxSmartPtr<eachClass> & Take(eachClass * ptr) 
    { FrDispose(mPtr); mPtr = ptr; return *this; } 


// Comparison operators compare the pointers contained in each 
FxBool operator == (const FxSmartPtr & inPtr) const 
    { return (mPtr == inPtr.mPtr); } 
FxBool operator == (const eachClass * inPtr) const 
    { return (mPtr == inPtr); } 
FxBool operator != (const FxSmartPtr & 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->AddReference(); } 
// Safely dispose one reference count. 
void Dispose(void) { if (mPtr != NULL) 
    // JASON/INDIE - [email protected] 
    // { ULONG refs = mPtr->GetRefCount(); mPtr->RemoveReference(); if (refs <= 1) mPtr = NULL; } } 
    { FxUInt32 refs = mPtr->GetRefCount(); mPtr->RemoveReference(); if (refs <= 1) mPtr = NULL; } } 

protected: 

    eachClass *mPtr; 
}; 
+0

FxLayerList是如何定義的? – Fox32 2011-05-18 18:20:43

+0

嘗試類似* lastVisible!= NULL – snoofkin 2011-05-18 18:21:17

+0

如果FxLayerList是一個向量,則在某些實現中,迭代器可能是普通指針。正確的方法是初始化它並測試end()。 – 2011-05-18 18:37:49

回答

4

它看起來是lastVisible是一個對象,而不是一個指針。如果您將某個對象與某個對象進行比較,那麼它必須具有適當的操作符。

也許這會編譯?

FxLayerList::iterator lastVisible = mBranch.end(); 
for (FxLayerList::iterator iter = mBranch.begin(); iter != mBranch.end(); iter++) 
{ 
    if ((*iter)->IsVisible() && !(*iter)->IsBypass()) 
    { 
     lastVisible = iter; 
    } 
} 
if (lastVisible != mBranch.end() && (*lastVisible)->GetGeneratedImage()) 
{ ... 

或者,如果FxLayerList只是一個指針集合FxLayer,這將是更直白:

FxLayer *lastVisible = NULL; 
for (FxLayerList::iterator iter = mBranch.begin(); iter != mBranch.end(); iter++) 
{ 
    if ((*iter)->IsVisible() && !(*iter)->IsBypass()) 
    { 
     lastVisible = *iter; 
    } 
} 
if (lastVisible != NULL && lastVisible->GetGeneratedImage()) 
{ ... 

答到UPDATE:見下面我的意見。問題(編譯器錯誤信息)可以通過明確地從「智能」指針檢索指針來解決:

FxBool FxLayerList::Contains(FxLayer *layer) const 
{ 
    for (FxLayerList::const_iterator iter=this->begin(); iter != this->end(); iter++) 
    { 
     if (iter.Get() == layer) { 
      return true; 
     } 
    } 
    return false; 
} 
+0

他在對象上調用函數的方式表明他使用指針:(* lastVisible) - > GetGeneratedImage()) – Fox32 2011-05-18 18:24:18

+0

@ Fox32 - 迭代器像指針一樣使用。他們不是空的。 – 2011-05-18 18:35:05

+0

*和 - >都是可重載的,所以可能不是。 – 2011-05-18 18:37:49

0

標準的運營商是重載的直接支持比較對象,但開箱即用,他們不知道比較什麼(我認爲的默認行爲是簡單地比較對象的地址,但你的錯誤似乎與此相矛盾)。

無論如何,How to compare two objects (the calling object and the parameter) in a class?似乎是一個非常類似的問題。

在你的類,你也許應該補充的東西像什麼亞歷山大以上建議:

int Date :: Compare (const Date& d) { 

    if (year<d.year) { 
    return -1; 
    } 
} 

bool operator == (const Date& d) const { 
    return !Compare(d); 
} 

當然修改,以滿足您的要求比較。