2010-03-23 73 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