在我们所有的C++课程,所有的老师总是把using namespace std;在之后#include在它们的S .h文件.从那时起,我觉得这很危险,因为在另一个程序中包含该标题我会将命名空间导入到我的程序中,可能没有意识到,打算或想要它(标题包含可以非常深入嵌套).
所以我的问题是双重的:我是对的,using namespace不应该在头文件中使用,和/或是否有某种方法来撤消它,例如:
//header.h
using namespace std {
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
还有一个问题是:标题文件是否应该是#include相应.cpp文件所需的所有标题,只有那些标题定义所需的标题,并让.cpp文件#include为其余标题,或者没有,并声明它需要的所有内容extern?
问题背后的原因与上面相同:在包含.h文件时我不想要惊喜.
另外,如果我是对的,这是一个常见的错误吗?我的意思是在现实世界的编程和"真正的"项目中.
谢谢.
是否需要#include某个文件,如果在标题(*.h)内,使用此文件中定义的类型?
例如,如果我使用GLib并希望gchar在我的标题中定义的结构中使用基本类型,是否有必要执行a #include <glib.h>,知道我已经在我的*.c文件中有它?
如果是,我还必须把它放在#ifndef和#define之后#define?
我有一个包含多个文件的C程序,所以我有,例如,stuff.c它实现了一些函数,并stuff.h使用函数原型.
我该如何记录评论中的功能?
我应该在头文件中包含所有文档,文件中的所有文档.c,还是复制两者的文档?我喜欢后一种方法,但后来我遇到问题,我将更新其中一个文档而不是另一个(通常是我进行第一次修改的文件,即如果我先修改头文件,那么它的注释)将反映这一点,但如果我更新实施,只会更改那些评论).
这个问题及其答案也适用于C++代码 - 另请参阅我应该在哪里放置文档注释?
我对头文件的使用有以下疑问.
1 - 在评论后加入警卫
/* Copyright Note and licence information (multiple lines) */
#ifndef FOO_H
#define FOO_H
// Header file contents
#endif
Run Code Online (Sandbox Code Playgroud)
Herb Sutter在他的"C++编码标准"一书中说,像上面这样的代码是有问题的.他说"#ifndef"语句应该出现在头文件的第一行.我觉得这并不令人信服.在头文件中你跟着你们/ gals吗?
2 - 在头文件中使用名称空间
#ifndef FOO_H
#define FOO_H
namespace FooNameSpace{
// Header file contents
}
#endif
Run Code Online (Sandbox Code Playgroud)
上面的代码是否使用了正确的做法?我的意思是,你在头文件中使用命名空间吗?我知道为什么在头文件中导入命名空间是没有意义的,但是如上所述的声明呢?
如果上面的方法是正确的,那么如何对另一个名称空间中的类进行" 转发声明 "呢?是不是
#ifndef FOO_H
#define FOO_H
namespace AnotherNameSpace{
class AnotherFoo; // forward declaration
}
namespace FooNameSpace{
// Use AnotherFoo here
}
#endif
Run Code Online (Sandbox Code Playgroud)
" 前向声明 "是避免" 循环依赖 " 的唯一方法,对吗?
我有很多.c文件,即实现文件说
来自任何文件的函数可以从不同的文件中调用任何函数.我的问题是,我是否需要.h为A和B的每个实现提供一个ie头文件,其中每个头文件都有A或B 中所有函数的定义.
此外,main.c中会有两个A.h,并B.h #included在里面?
如果有人能够最终说清楚,那么我如何在以后编译并运行终端中的多个文件.
谢谢.
当我尝试运行此代码时:
Instructor.cpp:
#include "Instructor.h"
#include "Person.h"
using std::string;
Instructor::Instructor() {
Person();
salary = 0;
}
Instructor::Instructor(string n, string d, string g, int s) {
Person(n, d, g);
salary = s;
}
void Instructor::print() {
if (gender == "M")
std::cout << "Mr. ";
else
std::cout << "Ms. ";
Person::print();
std::cout << " Instructor, Salary: " << salary << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
Instructor.h:
#include <iostream>
#include <string>
class Instructor: public Person
{
public:
Instructor();
Instructor(std::string n, std::string d, std::string g, int s);
virtual …Run Code Online (Sandbox Code Playgroud) 我有一个使用CMake构建并运行良好的项目.我的项目设置如下:
??? CMakeLists.txt
|
??? include/
? ??? standalone/
? ??? x.hpp
|
??? src/
??? standalone/
??? main.cpp
Run Code Online (Sandbox Code Playgroud)
我的标题的内容是这样的:
// ------x.hpp--------
#pragma once
#include <iostream>
class X
{
public:
void hello()
{
std::cout << "Hello World!\n"; // needs <iostream>
}
};
// -------main.cpp-------
#include <standalone/x.hpp>
int main()
{
X x;
x.hello();
}
Run Code Online (Sandbox Code Playgroud)
我使用以下内容 CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(standalone)
###############################################################
# Compiler settings
###############################################################
# use C++11 features
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# set warning level
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -pedantic -pedantic-errors …Run Code Online (Sandbox Code Playgroud) c++ cmake header-files buildconfiguration build-dependencies
好吧不确定这是正确的方式,甚至是正确的方式,但我已经看到了并开始使用它,假设您有6个文件
main.cpp
main.h
car.cpp
car.h
speed.cpp
speed.h
Run Code Online (Sandbox Code Playgroud)
c++ ×5
header-files ×5
c ×3
cmake ×1
comments ×1
compilation ×1
header ×1
include ×1
namespaces ×1
netbeans ×1