2013-08-27 38 views
0

我正在嘗試使用C++進行引導。我有一個Bootstrapping類進行採樣計算,一個Sample類存儲結果:C++包含的類無法被其他類識別

Sample.h文件

class Sample 
{ 
     // do something 
}; 

Bootstrapping

#include <vector> 
using namespace std; 

class Bootstrapping 
{ 
private: 
    vector<Sample> sample_list; // Here the problem happens 

    // do something 
}; 

main.cpp文件

#include <iostream> 
#include "Bootstrapping.h" 
#include "Sample.h" 
using namespace std; 

int main() 
{ 
    // do something 
} 

當我調試上述代碼時,編譯器在Bootstrapping類中彈出錯誤消息,該錯誤消息說identifier "Sample" is undefined。但我明顯已將它包含在main

任何人都可以幫我解決這個問題嗎?提前謝謝了。

+0

您是否在引導類聲明標頭中包含Sample.h? – darmat

回答

2

您應該重新排列標題。

#include "Sample.h" 
#include "Bootstrapping.h" 
+9

這是有效的,但依賴於頭部被包含在一個特定的順序是不好的做法。由於'Bootstrapping'的定義取決於'Sample'的定義,Bootstrapping.h應該包含Sample.h。 (前向聲明在這裏不起作用。) – bames53

2

您需要#include "Sample.h"Bootstrapping.h文件。

0

在Bootstrapping.h之後包含Sample.h。編譯器從頭到尾處理代碼,因此它在處理Bootstrapping類的聲明時對Sample類一無所知。 當然,您應該在Bootstrapping.h中包含Sample.h,以使此標題獨立於標題順序。