2011-10-10 118 views
3

我在Xcode一個Objective-C++項目,編譯好於正常的構建方案時,但是當我編譯歸檔,分析或簡介我得到的編譯錯誤:類類型範圍

必須使用「類」標籤指向型「行」在此範圍

這是一個非常簡化的版本我的代碼:

class Document; 

class Line 
{ 
public: 
    Line(); 

private: 
    friend class Document; 
}; 

class Document 
{ 
public: 
    Document(); 

private: 
    friend class Line; 
}; 

的錯誤發生在任何地方我嘗試使用型線。例如。

Line *l = new Line(); 

你知道如何解決此錯誤信息,爲什麼只在上面列出的方案之一編譯時會出現?

+3

「編譯其中文獻的方法中實現的document.mm文件時發生的錯誤」但是你決定我們不需要看到它。 –

+0

好點,我會修改。 – CD1212

+0

你是否在實現文件中包含類定義? –

回答

1

我設法通過將'Line'類型的名稱重構爲其他東西來解決這個問題。我能想到的唯一解釋是,執行和歸檔時所建,在Xcode中定義的另一個「行」中輸入一些外部源編譯。因此它需要'類'說明符來澄清類型。

2

這不回答你的問題,但看到因爲這是沒有答案與提供我只是提出這個意見的信息。而不必Document是朋友或LineLine是的Document的朋友,你可以有Document包含線,給我讓更多的意義,似乎更好的封裝。

class Line 
{ 
public: 
    Line(); 
}; 

class Document 
{ 
public: 
    Document(); 

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

我在我的代碼有這個問題。在查看生成的預處理文件後,我發現我的類名與函數名相同。所以編譯器試圖通過在類型前添加類標籤來解決歧義。

代碼(有錯誤)之前:

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

代碼後:

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

同樣的問題。我在一個節儉文件中犯了這個錯誤,生成的文件有編譯錯誤。 – CCoder