c++ - 如何确定是什么*真正*导致您的编译器错误

标签 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

最佳答案

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

例如,在给定的情况下,编译器可能会在到达 CP_M_ReferenceCounted 时停止解析声明的类型,并将其解析为要声明的名称。语法允许这样做,因为一些声明没有给出类型(构造函数就是一个例子)。所以它需要一个该名称的初始值设定项,而不是星号。这暗示 CP_M_ReferenceCounted 可能未声明。检查您包含了正确的 header 。

关于c++ - 如何确定是什么*真正*导致您的编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2503739/

相关文章:

c++ - 在类声明之前在模板内进行 `using` 声明

python - 如何在Cheetah for Python中正确使用多维字典?

c++ - 为什么相同的模板类会得到相同的静态 ID?

c - C 中的内联实际上是如何减慢该程序的速度的?

compiler-construction - 基于堆栈的字节码或无限寄存器机的优点

c++ - Z3打印评估结果

c++ - 非阻塞套接字在 Windows 上丢失数据

c++ - 在 NetBeans 中编译基于 Qt 的 C++ 代码时出错

c++ - 奇怪的 visual studio 2008 C++ 编译器错误

c++ - 如何防止 qmake 在此 "moc"构建方案中创建额外的 'out of source' 文件夹?