xcode 类类型范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7715801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class type scope
提问by cdyer
I have an Objective-C++ project in Xcode which compiles fine when on the normal build scheme, but when I compile for Archive, Analyze or Profile I get the compile error:
我在 Xcode 中有一个 Objective-C++ 项目,它在正常构建方案下编译得很好,但是当我为 Archive、Analyze 或 Profile 编译时,我得到了编译错误:
Must use 'class' tag to refer to type 'Line' in this scope
必须使用“class”标记来引用此范围内的“Line”类型
This is a very much simplified version of my code:
这是我的代码的一个非常简化的版本:
class Document;
class Line
{
public:
Line();
private:
friend class Document;
};
class Document
{
public:
Document();
private:
friend class Line;
};
The errors occur anywhere I try to use the type Line. Eg.
错误发生在我尝试使用类型 Line 的任何地方。例如。
Line *l = new Line();
Do you know how to fix this error message and why it only appears when compiling in one of the schemes listed above?
您知道如何修复此错误消息以及为什么它仅在使用上面列出的其中一种方案进行编译时才会出现吗?
采纳答案by cdyer
I managed to fix the issue by refactoring the 'Line' type name into something else. The only explanation I can think of is that when performing and Archive build, Xcode compiles in some external source which defines another 'Line' type. Hence it needed the 'class' specifier to clarify the type.
我设法通过将“行”类型名称重构为其他名称来解决该问题。我能想到的唯一解释是,在执行和存档构建时,Xcode 在定义另一种“行”类型的某些外部源中进行编译。因此它需要“类”说明符来阐明类型。
回答by chunkyguy
I had this problem in my code. After looking at the generated preprocessed file I found the one of my class name was same as a function name. So compiler was trying to resolve the ambiguity by asking to add class tag in front of the type.
我的代码中有这个问题。查看生成的预处理文件后,我发现我的类名之一与函数名相同。所以编译器试图通过要求在类型前面添加类标记来解决歧义。
Before code (with error):
代码之前(有错误):
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_);
After code:
代码后:
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_);
回答by AJG85
This doesn't answer your question but seeing as that's unanswerable with the information provided I'll just make this suggestion. Instead of having Document
be a friend or Line
and Line
being a friend of Document
you could have Document
contain lines which to me makes a bit more sense and seems better encapsulated.
这并不能回答您的问题,但鉴于所提供的信息无法回答您的问题,因此我将提出此建议。而不必Document
是朋友或Line
和Line
是朋友Document
你可以有Document
包含线,给我让更多的意义,而且似乎更好封装。
class Line
{
public:
Line();
};
class Document
{
public:
Document();
private:
std::vector<Line> m_lines;
};