如何确定*真正*导致编译器错误

时间:2010-03-23 21:24:00

标签: c++ templates compiler-construction function

我正在移植一个非常大的代码库,我对旧代码的处理更加困难。

例如,这会导致编译器错误:

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'的模板声明

就是这样。我该如何判断这是什么?我确实生锈了模板。

更新:

这是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

4 个答案:

答案 0 :(得分:3)

我认为您必须对编译器的错误消息产生某种感觉。有更糟糕的,有更好的编译器。那个肯定是最糟糕的一个。一个好的指针将插入符号指向发生错误的位置,并提示可能出错的地方。

例如,在给定的情况下,编译器在到达CP_M_ReferenceCounted时可能会停止解析声明的类型,并将其解析为要声明的名称。语法允许,因为一些声明没有给出类型(构造函数是一个例子)。所以它期望该名称的初始化程序,而不是明星。这暗示CP_M_ReferenceCounted可能未被声明。检查您是否包含正确的标题。

答案 1 :(得分:2)

我不会回答你的“大图片”问题,但你的第一个错误看起来很简单。

在第一个片段中,我的猜测是CP_M_ReferenceCounted类尚未声明。您可能忘记包含“CP_M_ReferenceCounted.h”或一些类似命名的文件。编译器告诉您它没有找到可以应用*(指针)修饰符的类型名称,这意味着它不会将CP_M_ReferenceCounted识别为有效的类型名称。这意味着缺少声明,这反过来可能意味着缺少头文件包括。

第二个错误对我来说无疑是一个谜。我使用“typename”而不是类,因为你使用eachClass作为指针,但即使它会增加清晰度,技术上也不应该有任何区别。

答案 2 :(得分:2)

其他人正在解决您的具体错误,所以我会尝试解决大局。

Clang project的目标之一是拥有more helpful diagnostics。 Clang的C++ support不完整,但根据您的代码推动C ++语言边界的程度,它可能足以为您提供良好的错误消息。

对于STL中的模板,请尝试使用STLFilt。我已经多次使用它了,它可以很好地清理多行错误消息,杂乱的默认模板参数,并用可以理解的东西替换它们。我不确定它对STL之外的模板有多大作用,但它可能值得一试。

答案 3 :(得分:0)

您已使用CP_M_ReferenceCounted向我们展示了一些问题代码,但向我们展示了CP_M_RefCounted的标头文件。这看起来像是错误的标题,或者你拼错了类名。

此外,请勿在标题中使用前导下划线包含警戒。资本化和下划线 - 而不是空格是可以的:H_CP_M_RefCountedH_CP_M_REFCOUNTEDCP_M_REFCOUNTED_H