c – 如何确定导致编译器错误的*真正*
发布时间:2020-12-16 09:26:52 所属栏目:百科 来源:网络整理
导读:我正在移植一个非常大的代码库,我在使用旧代码时遇到了更多困难. 例如,这会导致编译器错误: inline CP_M_ReferenceCounted *FrAssignRef(CP_M_ReferenceCounted * to,CP_M_ReferenceCounted * from){ if (from) from-AddReference(); if (to) to-RemoveRefe
我正在移植一个非常大的代码库,我在使用旧代码时遇到了更多困难.
例如,这会导致编译器错误: 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.检查您是否包含正确的标题. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |