类类型范围

cdy*_*yer 3 c++ xcode objective-c++

我在Xcode中有一个Objective-C++项目,它在正常的构建方案上编译得很好,但是当我为Archive,Analyze或Profile编译时,我得到了编译错误:

必须使用'class'标记来引用此范围中的'Line'类型

这是我的代码的一个非常简化的版本:

class Document;

class Line
{
public:
    Line();

private:
    friend class Document;
};

class Document
{
public:
    Document();

private:
    friend class Line;
};
Run Code Online (Sandbox Code Playgroud)

错误发生在我尝试使用Line类型的任何地方.例如.

Line *l = new Line();
Run Code Online (Sandbox Code Playgroud)

你知道如何解决这个错误信息以及为什么它只在编译上面列出的一个方案时出现?

chu*_*guy 7

我的代码中遇到了这个问题.在查看生成的预处理文件后,我发现我的一个类名与函数名相同.所以编译器试图通过要求在类型前面添加类标记来解决歧义.

代码之前(有错误):

template <typename V>
void Transform(V &slf, const Transform &transform){ // No problem
//... stuff here ...
}

void Transform(V2 &slf, const Transform &transform); // Error: Asking to fix this

void Transform(V2 &slf, const class Transform &transform); // Fine

//Calling like
Transform(global_rect, transform_);
Run Code Online (Sandbox Code Playgroud)

代码后:

template <typename V>
void ApplyTransform(V &slf, const Transform &transform){ // No problem
//... stuff here ...
}

void ApplyTransform(V2 &slf, const Transform &transform);

//Calling like
ApplyTransform(global_rect, transform_);
Run Code Online (Sandbox Code Playgroud)

  • 当您将结构/类成员命名为与结构/类名称相同的名称时,会发生同样的问题。我在一个 thrift 文件中犯了这个错误,生成的文件有编译错误。 (2认同)
  • 另外,如果您在枚举中有类名。 (2认同)