'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 Document;
};

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?

c++


Solution 1:[1]

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

Solution 2:[2]

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;
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chunkyguy
Solution 2 AJG85