我想編譯我的代碼,但我得到一個類的錯誤。其中一個類編譯得很好(Example.h),但另一個(Name.h)一直給我這個類沒有名稱類型錯誤。我認爲這與循環依賴有關,但如何在沒有前向減速的情況下解決這個問題?類名不聲明類型C++
Name.h
#ifndef _NAME
#define _NAME
#include <iostream>
#include <string>
using namespace std;
#include "Example.h"
class Name{
};
#endif
example.h文件
#ifndef _EXAMPLE
#define _EXAMPLE
#include <iostream>
#include <string>
using namespace std;
#include "Name.h"
class Example{
};
#endif
我看到了一個有關使用正向減速後,但我需要從實例類訪問承包商,客人..
您不能在'A.h'中的'B.h'和'B.h'中包含'A.h'。它確實是循環依賴。您可以嘗試前進類的聲明。 – Mikhail
但是將向前聲明讓我訪問其他類的成員函數? – user2351234
與問題無關,但由於您(大概)使用現代C++編譯器,因此可以避免使用過時的'#ifndef _EXAMPLE #define _EXAMPLE#endif'事物。只需使用'#pragma once'。 (http://en.wikipedia.org/wiki/Pragma_once) –