2010-03-23 30 views
3

我正在移植一個非常龐大的代碼庫,並且在舊代碼中遇到了更多的困難。如何確定導致編譯器錯誤的*真* *

例如,這將導致一個編譯器錯誤:

inline CP_M_ReferenceCounted * 
FrAssignRef(CP_M_ReferenceCounted * & to, CP_M_ReferenceCounted * from) 
{ 
    if (from) from->AddReference(); 
    if (to) to->RemoveReference(); 
    to = from; 
    return to; 
} 

的錯誤是:錯誤:預期初始化之前 '*' 標記。

我怎麼知道這是什麼。我查了內聯成員函數,以確保我明白,我不認爲內聯是原因,但我不確定是什麼。

又如:

template <class eachClass> 
    eachClass FrReferenceIfClass(FxRC * ptr) 
    { 
     eachClass getObject = dynamic_cast<eachClass>(ptr); 
     if (getObject) getObject->AddReference(); 
     return getObject; 
    } 

的錯誤是:錯誤:模板聲明 'eachClass FrReferenceIfClass'

這是所有。我如何確定這是什麼?我承認模板生鏽。

UPDATE:

這裏是CP_M_ReferenceCounted:

#pragma once 
#ifndef _H_CP_M_RefCounted 
#define _H_CP_M_RefCounted 

// CPLAT_Framework 
#include "CP_Types.h" 

CPLAT_Begin_Namespace_CPLAT 

/*! 
* @class  CP_M_RefCounted 
* @brief  Mix-in class for objects that are reference counted. 
*/ 

class CP_EXPORT CP_M_RefCounted 
{ 
public: 
    //! @name Reference 
    //@{ 
      UInt32      AddReference() const; 
      UInt32      RemoveReference() const; 
//@} 

//! @name Autorelease 
//@{ 
     void      Autorelease() const; 
//@} 

//! @name Getters 
//@{ 
            /*! 
            * Returns the current ref count. 
            * 
            * @exception none 
            * 
            * @return  UInt32   The current referencce count. 
            */ 
     UInt32      GetRefCount() const         { return(fRefCount); } 
//@} 

//! @name operators 
//@{ 
     CP_M_RefCounted&   operator = (const CP_M_RefCounted& inRefCounted); 
//@} 

protected: 
    //! @name Constructor/Destructor 
    //@{ 
    //! Constructor. 
            CP_M_RefCounted(); 
            CP_M_RefCounted(CP_M_RefCounted& inRefCounted); 
//! Destructor. 
virtual        ~CP_M_RefCounted(); 
//@} 

// class data 
private: 
mutable UInt32      fRefCount; /*! The number of references to this object. */ 

//======================================================================== 
// Platform specific routines 
//======================================================================== 
#if TARGET_OS_MAC 
#endif 

#if TARGET_OS_WIN32 
#endif 

#if TARGET_OS_LINUX 
#endif 
}; 

template <class T> 
inline const T* CP_Autorelease(const T* inObj) 
{ 
    if(inObj) 
     inObj->Autorelease(); 

    return(inObj); 
} 

template <class T> 
inline T* CP_Autorelease(T* inObj) 
{ 
    if(inObj) 
     inObj->Autorelease(); 

    return(inObj); 
} 

    /*! 
    * @class CP_SmartRef 
    * @brief Template class representing a smart pointer for reference counted objects. 
    */ 
    template <class T> 
    class CP_SmartRef 
    { 
    public: 
     //! @name Constructor/Destructor 
     //@{ 
     //! Constructor. 
           CP_SmartRef() 
             : fObj(NULL)         {} 
            CP_SmartRef(
              T *inObj, 
              bool inTransferOwnership=false) 
               : fObj(inObj)       { if(!inTransferOwnership && fObj) fObj->AddReference(); } 
            CP_SmartRef(const CP_SmartRef<T>& inRef) 
             : fObj(inRef.fObj)        { if(fObj) fObj->AddReference(); } 
            template <class Other> 
            CP_SmartRef(const CP_SmartRef<Other>& inRef) 
             : fObj(NULL)         { T* other = inRef.Get(); this->Reset(other); } // assignment to local variable should prevent upcasts and cross-casts 
//! Destructor. 
            ~CP_SmartRef()          { if(fObj) fObj->RemoveReference(); } 
//@} 

//! @name operators 
//@{ 
     T&       operator *() const         { return(*fObj); } 
     T*       operator->() const         { return(fObj); } 

            operator T *() const        { return(fObj); } 

     CP_SmartRef<T>&    operator = (const CP_SmartRef<T>& inRef)   { this->Reset(inRef.fObj); return *this; } 
     template <class Other> 
     CP_SmartRef<T>&    operator = (const CP_SmartRef<Other>& inRef)  { this->Reset(inRef.Get()); return *this; } 
     CP_SmartRef<T>&    operator = (T* inObj)        { this->Reset(inObj); return *this; } 
     template <class Other> 
     CP_SmartRef<T>&    operator = (Other* inObj )       { this->Reset(inObj); return *this; } 
    //@} 

    //! @name Object management 
    //@{ 
      T       *Get()  const          { return(fObj); } 
      T       *Reset(
              T  *inObj, 
              bool  inTransferOwnership = false); 
      T       *Release(); 
    //@} 


    // class data 
protected: 
     T        *fObj; 

//======================================================================== 
// Platform specific routines 
//======================================================================== 
#if TARGET_OS_MAC 
#endif 

#if TARGET_OS_WIN32 
#endif 

#if TARGET_OS_LINUX 
#endif 
}; 

template <class T> 
T* CP_SmartRef<T>::Reset(T *inObj, bool inTransferOwnership) 
{ 
    if (inObj != fObj) 
    { 
     if(fObj) 
      fObj->RemoveReference(); 

     fObj = inObj; 

     if(inObj && !inTransferOwnership) 
      inObj->AddReference(); 
    } 
    else if(inObj && inTransferOwnership) 
    { 
     inObj->RemoveReference(); 
    } 

    return(fObj); 
} 

template <class T> 
T* CP_SmartRef<T>::Release() 
{ 
    T *tmp = fObj; 

    fObj = NULL; 

    return(tmp); 
} 

CPLAT_End_Namespace_CPLAT 

#endif // _H_CP_M_RefCounted 
+0

您使用的編譯器是什麼? – 2010-03-23 21:37:59

+0

@Josh - XCode 3.2 1,但GCC 4.0 – 2010-03-23 21:42:00

+1

@ML我不能相信在第二種情況下,這是整個編譯器錯誤輸出。聽起來更像是另一條消息的後續。 – 2010-03-23 22:04:34

回答

3

我認爲你必須開發一些那種感覺,你的編譯器的錯誤信息。有更糟的,有更好的編譯器。那肯定是更糟糕的一個。一個好的人指出發生錯誤的地方,並提示什麼可能是錯誤的。

例如,在給定的情況下,編譯器可能會在到達CP_M_ReferenceCounted時停止解析聲明的類型,並將其解析爲要聲明的名稱。語法允許這樣做,因爲某些聲明沒有給出類型(構造函數就是一個例子)。所以它期望一個名字的初始化器,而不是一個明星。這暗示CP_M_ReferenceCounted可能未被聲明。檢查你是否包含正確的標題。

+0

@Johannes - 我確實包含了正確的標題。我也做了:typedef CP_M_ReferenceCounted FxRC;並且這給了我:錯誤:'CP_M_ReferenceCounted'沒有命名一個類型 – 2010-03-23 21:46:10

+1

@ML:也可能有圓形包含,並且聲明不可見。 – UncleBens 2010-03-23 23:37:15

2

我不會回答你的「大圖片」問題,但你的第一個錯誤看起來很簡單。

在第一個片段中,我的猜測是CP_M_ReferenceCounted類尚未聲明。您可能忘記包含「CP_M_ReferenceCounted.h」或一些類似命名的文件。編譯器告訴你,它沒有找到它可以應用*(指針)修飾符的類型名稱,這意味着它不會將CP_M_ReferenceCounted識別爲有效的類型名稱。這意味着缺少聲明,這反過來可能意味着缺少頭文件包含。

第二個錯誤對我來說無疑是個謎。我將使用「typename」而不是class,因爲你使用eachClass作爲指針,但這在技術上不應該有任何區別,即使它會增加清晰度。

+0

我這樣做:#include「CP_M_ReferenceCounted.h」。 – 2010-03-23 21:37:37

+0

使用template + argument-list語法聲明函數模板是無效的。這是保留專業化。在你的情況下,你可以期望像「FrReferenceIfClass不是模板」這樣的東西被髮射。這是因爲對於一個名稱後面跟着一個模板參數列表,這個名字必須被稱爲模板(這是爲什麼需要somethines':: template'消歧的原因)。他的模板聲明對我來說看起來很好 - 我懷疑他的錯誤在其他地方(也許他試圖以無效的方式使用或超載)。 – 2010-03-23 21:43:58

+0

#Drew:加入會導致這種錯誤:在'<'token之前的預期初始化器 – 2010-03-23 21:44:20

2

其他人正在解決您的具體錯誤,所以我會嘗試解決大局。

Clang project的目標之一是有more helpful diagnostics。 Clang的C++ support是不完整的,但取決於您的代碼推動C++語言的邊界多少,它可能足以爲您提供良好的錯誤消息。

對於STL中的模板,請嘗試使用STLFilt。我已經多次使用過它,它在清理多行錯誤消息方面做得非常出色,它使用默認的模板參數混雜在一起,並將它們替換爲可以理解的東西。我不確定它對STL以外的模板有多少作用,但它可能值得一試。

0

您向我們展示了一些使用CP_M_ReferenceCounted的問題代碼,但向我們展示了CP_M_RefCounted的頭文件。這看起來像錯誤的標題,或者你拼錯了類名。

此外,請勿在您的標題中使用前導下劃線包括警衛。大寫字母和下劃線代替空格即可:H_CP_M_RefCountedH_CP_M_REFCOUNTEDCP_M_REFCOUNTED_H

+0

@quamrana - 不,如果我看看CP_ReferenceCounted.h,那就是它。 – 2010-03-23 23:00:25

+0

@ML:因此,如果CP_ReferenceCounted.h中有類「CP_M_RefCounted」,爲什麼你的代碼片段使用「CP_M_ReferenceCounted」而不是「CP_M_RefCounted」? – quamrana 2010-03-23 23:18:24

+0

將.h文件名更改爲C_M_RefCounted。h和typedef到CPLAT :: CP_M_RefCounted和viola!謝謝。 – 2010-03-23 23:23:19

相關問題