C++ 类类型范围
我在Xcode中有一个Objective-C++项目,它在正常构建方案下可以很好地编译,但是当我编译归档、分析或概要文件时,我得到了编译错误: 必须使用“class”标记来引用此范围中的类型“Line” 这是我的代码的一个非常简化的版本:C++ 类类型范围,c++,xcode,objective-c++,C++,Xcode,Objective C++,我在Xcode中有一个Objective-C++项目,它在正常构建方案下可以很好地编译,但是当我编译归档、分析或概要文件时,我得到了编译错误: 必须使用“class”标记来引用此范围中的类型“Line” 这是我的代码的一个非常简化的版本: class Document; class Line { public: Line(); private: friend class Document; }; class Document { public: Document()
class Document;
class Line
{
public:
Line();
private:
friend class Document;
};
class Document
{
public:
Document();
private:
friend class Line;
};
错误发生在我尝试使用类型行的任何地方。例如
Line *l = new Line();
您知道如何修复此错误消息,以及为什么仅在使用上面列出的方案之一进行编译时才会出现此错误消息?这并不能回答您的问题,但鉴于所提供的信息无法回答此问题,我仅提出此建议。与其让
Document
成为朋友或Line
和Line
成为Document
的朋友,您可以让Document
包含行,这些行对我来说更有意义,而且看起来更容易封装
class Line
{
public:
Line();
};
class Document
{
public:
Document();
private:
std::vector<Line> m_lines;
};
类行
{
公众:
第()行;
};
类文档
{
公众:
文件();
私人:
std::向量m_线;
};
我通过将“Line”类型名称重构为其他名称,成功地解决了这个问题。我能想到的唯一解释是,在执行和归档构建时,Xcode在定义另一种“行”类型的外部源代码中编译。因此它需要“类”说明符来澄清类型。我的代码中有这个问题。查看生成的预处理文件后,我发现其中一个类名与函数名相同。所以编译器试图通过要求在类型前面添加class标记来解决歧义
前代码(有错误):
模板
void变换(V&slf、const变换和Transform){//没问题
//…这里的东西。。。
}
无效变换(V2&slf、常量变换&Transform);//错误:请求修复此问题
无效转换(V2&slf,常量类转换与转换);//好的
//像打电话一样
变换(全局,变换);
代码后:
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_);
模板
void ApplyTransform(V&slf,const-Transform&Transform){//没问题
//…这里的东西。。。
}
无效应用转换(V2和slf、常量转换和转换);
//像打电话一样
应用转换(全局、转换);
“编译document.mm文件时出错,其中实现了document的方法。”但您决定我们不需要查看它。您是否在实现文件中包含类定义?是的。它似乎知道类型是什么,只是出于某种原因希望我每次都在它前面声明类。当您将结构/类成员命名为与结构/类名相同时,也会出现相同的问题。我在一个旧文件中犯了这个错误,生成的文件有编译错误。另外,如果枚举中有类名。
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_);