#include C / C ++中的标题 [英] #include headers in C/C++

查看:153
本文介绍了#include C / C ++中的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了几个关于编译问题(特别是C ++)的问题,并注意到在许多情况下,问题是缺少头#include。我不禁想到我的无知,问自己(现在给你):



为什么丢失的标题不会被自动检查和添加或请求程序员



这种功能适用于Netbeans中的Java导入语句。

解决方案

记住 java.util.Date java.sql.Date ?如果有人在他们的代码中使用 Date ,你不能知道他们是否忘记了 import java.util.Date import java.sql.Date



在Java和C ++中, / include语句丢失。所以两种语言都尝试。您的IDE可能会为您的代码中使用的未声明的符号提供建议。



C ++中的问题更复杂,因为标准说任何标准头可以包括任何其他标准头(s)。因此,很容易使用函数或类而不直接包含定义它的头,因为你的编译器恰好包括间接的头。在一些实现中,结果代码可以工作,但不是其他实现,根据它们是否共享该头依赖。



一般来说,C ++ IDE不可能判断头依赖是保证,或者只是用户不应该依赖的附带实施细节。显然,对于标准库,它只是知道什么是在什么头文件中定义,但一旦你到第三方库,它就变得不确定。



我认为大多数C ++程序员必须查找什么头定义什么符号。使用Java,单文件公用类文件规则大大简化了这一点,你只需导入你想要的包/类。 C ++没有包,并且IDE找到一个名为 my_namespace :: something :: MyClass 的类的唯一方法是在每个头文件中搜索它。 / p>

After reading several questions regarding problems with compilation (particularly C++) and noticing that in many cases the problem is a missing header #include. I couldn't help to wonder in my ignorance and ask myself (and now to you):

Why are missing headers not automatically checked and added or requested to the programmer?

Such feature is available for Java import statements in Netbeans for example.

解决方案

Remember the clash in Java between java.util.Date and java.sql.Date? If someone uses Date in their code, you can't tell whether they forgot import java.util.Date or import java.sql.Date.

In both Java and C++, it is not possible to tell with certainty what import/include statement is missing. So neither language tries. Your IDE might make suggestions for undeclared symbols used in your code.

The problem is further complicated in C++, because the standard says that any standard header can include any other standard header(s). It's therefore very easy to use a function or class without directly including the header which defines it, because your compiler happens to include the right header indirectly. The resulting code works in some implementations but not others, according to whether they share that header dependency.

It's not in general possible for a C++ IDE to tell whether a header dependency is "guaranteed", or just an incidental implementation detail that users shouldn't rely on. Obviously for standard libraries it could just know what's defined in what headers, but as soon as you get to third party libraries it gets quite uncertain.

I think most C++ programmers expect to have to look up what headers define what symbols. With Java, the one-public-class-per-file rule simplifies this considerably, and you just import the packages/classes you want. C++ doesn't have packages, and the only way for the IDE to find a class called my_namespace::something::MyClass is to search for it in every header file.

这篇关于#include C / C ++中的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆