开发者

Class type scope

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:

Must use 'class' tag to refer to type 'Line' in this scope

This is a very much simplified version of my code:

class Document;

class Line
{
public:
    Line();

private:
    friend class D开发者_如何学运维ocument;
};

class Document
{
public:
    Document();

private:
    friend class Line;
};

The errors occur anywhere I try to use the type Line. Eg.

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?


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_);


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.

class Line
{
public:
    Line();
};

class Document
{
public:
    Document();

private:
    std::vector<Line> m_lines;
};


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜