我在VS2012中使用預編譯頭文件中有一個非常基礎的項目。我知道我應該將所有「常用」包括到stdafx.h中,並且我需要將它包含在每個.cpp文件中。因此,基本設置是這樣的:VS2012預編譯頭文件:我的.h文件如何知道包含在stdafx.h中?
了foo.h:
#ifndef FOO_H
#define FOO_H
class Foo {
public:
Foo();
~Foo();
void do(string str);
};
#endif
foo.c的:
#include "stdafx.h"
#include "Foo.h"
void Foo::do(string str) {}
Stdafx.h中:
#include <string>
using namespace std;
沒有預編譯頭,我將#include <string>
放入foo.h中,因爲它必須知道string
的聲明。但是這個foo.h在這個設置中怎麼知道string
? (請注意,stdafx.h僅包含在.cpp文件中)。
注意:我有一個使用預編譯頭文件的工作示例;問題是關於這是如何工作的。
樣式說明:在標頭文件中放置'using namespace std;'確實會污染全局名稱空間。使用明確的命名空間(例如,參考'std :: string') – Joe 2013-03-27 13:47:34
@Joe:只是爲了確保我理解正確:而不是'使用命名空間標準;'我應該使用'std:string'每當我需要一個字符串? – mort 2013-03-27 13:56:48