只使用库中的某些函数?
我只想使用 math.h 中的某些函数(不包括整个库),
例如,我需要使用“sqrt”和“exp”,但我有名为“y1”(可能还有其他)的变量,这些变量与math.h 中的定义
如何仅使用此类库中的某些函数?
我尝试过,
#define sqrt cmath::sqrt
但这没有用,我以前见过类似的东西
#define cout std::cout
,所以我认为它可能有用。
有什么想法吗?
i would like to use only certain functions from math.h (WITHOUT including the entire library)
for example, i need to use "sqrt" and "exp", but i have variables named "y1" (and possibly others) which conflict with definitions in math.h
how can i use only certain functions from a library like that?
i tried
#define sqrt cmath::sqrt
but that did not work, i have seen something like that before with
#define cout std::cout
i think, so i thought it might work.
any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将您的代码放在您自己的命名空间中。通过使用命名空间运算符 (::),您可以区分具有相同名称(并且位于同一范围内)的变量。
Put your code in your own namespace. By using namespace operator (::) you can distinguish variables with the same name (and which are in the same scope).
只需使用它们?
您还可以显式指定命名空间:
如果需要,您可以从命名空间引入特定名称,而无需引入整个命名空间。这是使用
using
关键字完成的。请不要为此创建#define
:Just use them?
You can also explicitly specify the namespace:
You can, if you want, bring in specific names from namespaces without bringing in the whole namespace. This is done using the
using
keyword. Please don't create a#define
for this:是的,您可以只使用您想要的部件。
只需为您需要访问的那些函数/全局变量创建您自己的部分 mini_cmath.h 标头(假设这些函数/全局变量不冲突!)。
正如一些人所指出的,没有办法只 #include 给定的块(除非包含的标头具有预处理器宏来启用这样的功能,例如 windows.h)
但是如果您只是声明您希望使用(正确)的那些函数,然后编译&链接(只要您的链接中包含必要的 .lib),那么您就成功了。
然而,更一般地说 - 全局变量一般来说是一个坏主意,但如果您绝对必须出于希望有效的原因使用它们,那么您应该将它们放在命名空间中,并在您的应用程序中引用它们。按完全限定名称来源:
Yes, you can just use the parts you want.
Simply create your own partial mini_cmath.h header for those functions / globals you need access to (assuming those don't conflict!).
As several have noted, there's no way to only #include a given chunk (unless the included header has preprocessor macros to enable such a thing, such as windows.h)
But if you simply declare those functions you wish to use (correctly), and then compile & link (as long as the necessary .lib is included in your link), then you're golden.
However, on a more general note - globals are a bad idea in general, but if you absolutely must use them for hopefully valid reasons, then you should be putting them in a namespace, and referencing them in your source by fully qualified name:
只需
#include
。如果您的变量名称是这样的问题,请重命名它们。您不能仅包含文件的一部分。Just
#include <cmath>
. If your variable names are such an issue, then rename them. You can't include just a piece of a file.编辑:
为什么使用您自己的命名空间。您仍然可以使用y1
应该与 math.h 中的任何内容冲突?sqrt
,并且可以通过mynamespace::y1
解析来访问命名空间中的内容。反正,
cmath
是头文件math.h
的别名。std
是一个命名空间,其中包含iostream.h
中的cout
对象。因此,虽然您可以使用 std::cout,但无法使用头文件进行相同的范围解析。您只需包含
math.h
或cmath
即可。并且您不需要为这样的 cout 使用
#define
。只需添加一个using namespace std
,您就不必解析范围。如果您担心的话,使用整个命名空间不会导致任何开销。干杯!
EDIT:
Why should aUse your own namespace. You'll still be able to usey1
conflict with anything in math.h?sqrt
and you can access stuff from your namespace by resolving throughmynamespace::y1
.Anyway,
cmath
is an alias of the header filemath.h
.std
is a namespace, which contains thecout
object iniostream.h
.So while you can use
std::cout
, you can't do the same scope resolution with a header file. You just have to includemath.h
, orcmath
.And you don't need to use a
#define
for cout like that. Just add ausing namespace std
and you won't have to resolve the scope.And using an entire namespace does not cause any overhead, if that's what you're concerned about. Cheers!
y1()
是 C 数学库的众多 POSIX 扩展之一。您可以通过在包含
之前取消定义以下宏来删除这些扩展:一般来说,避免将您自己的任何名称放入全局命名空间中也是一个非常好的主意,因为这样很有可能如果您使用任何 C 库,则会发生冲突。尽可能避免使用全局变量,如果必须使用全局变量,请将其放入您自己的命名空间中。
y1()
is one of a number of POSIX extensions to the C math library. You can remove these extensions by undefining the following macros before including<cmath>
:It's also a very good idea in general to avoid putting any of your own names in the global namespace, since there's a good chance of a collision if you use any C libraries. Avoid global variables whenever you can and, if you must use one, put it inside your own namespace.