【发布时间】:2014-11-10 07:58:29
【问题描述】:
我现在有点困惑,因为我计划第一次在我的一个项目中包含多个源文件和头文件。
所以我想知道这是否是正确的方法?
我必须在每个直接使用它的源文件中包含字符串头吗?
Visual C++ 希望我包含的“stdafx.hpp”标头又如何呢?
这就是要走的路吗?
main.cpp
#include "stdafx.hpp"
#include <string> //?
#include <stringLib1.h>
#include <stringLib2.h>
using std::string;
//use a windows.h function here
//use a stringLib1 function here
//use a stringLib2 function here
stringLib1.h
#include "stdafx.hpp"
#include <string>
using std::string;
class uselessClass1
{
public:
string GetStringBack1(string myString);
};
stringLib1.cpp
#include "stdafx.hpp"
string uselessClass1::GetStringBack1(string myString) {
return myString;
}
stringLib2.h
#include "stdafx.hpp"
#include <string>
using std::string;
class uselessClass2
{
public:
string GetStringBack2(string myString);
};
stringLib2.cpp
#include "stdafx.hpp"
string uselessClass2::GetStringBack2(string myString) {
return myString;
}
【问题讨论】:
-
是的,您必须在要使用的每个文件中包含头文件。但是,您不应在标题中使用
using关键字。这不是很好的风格。 -
@cell 我希望这是一个讽刺的评论,你不是认真的。
-
在每个包含标题的 c 文件中,
using也将包含在内。这可能会产生一些糟糕的命名冲突。 -
在头文件中添加
using语句是导致namespace pollution的最快方法之一 -
@Cyber 这不是风格问题。
标签: c++ include header-files standard-library precompiled-headers