VC++ 编译器和类型转换?

发布于 2024-07-17 09:10:51 字数 350 浏览 10 评论 0原文

当我将程序从 Mac 移动到这台 Windows PC 时,VC++ 2008 编译器在将无符号整数传递给 cmath pow() 函数时出现错误。 据我了解,该函数不会重载以接受除浮点数之外的任何内容。

是否有一些编译器标志/设置会忽略这些错误? 还有谁知道如何找到 VC++ 编译器的文档吗?

编辑

这不是警告,而是错误。 然而,对我来说这不是问题,因为我的程序只处理以整数形式出现的数字,所以我不在乎它们不是浮点数。 如果这只是警告,我会继续我的生活,但它不允许我编译。 我可以以某种方式抑制错误吗? 就像我说的,我的 Mac 上没有出现错误,程序也运行良好。

When I moved a program from a Mac to this Windows PC, the VC++ 2008 compiler is giving me errors for passing unsigned ints to the cmath pow() function. As I understand, this function is not overloaded to accept anything but floating-point numbers.

Is there some compiler flag/setting that will ignore these errors? Also does anyone know how to find the documentation on the VC++ compiler?

Edit

This isn't a warning, it's an error. However, for me it's not an issue since my program is only dealing with numbers that come out as integers, so I don't care that they aren't floats. If it was just warnings I would move on with my life, but it's not letting me compile. Can I suppress errors somehow? Like I said, the errors aren't coming up on my Mac and the program is fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

从﹋此江山别 2024-07-24 09:10:51

关于这里的其他答案,告诉问题作者关闭此警告并不是一个好主意。 他的代码被破坏了——他传递的是无符号整数而不是浮点数。 你应该告诉他修复他的代码!

这不是警告,而是错误。 然而,对我来说这不是问题,因为我的
程序只处理以整数形式出现的数字,所以我不在乎
它们不是花车。 如果这只是警告我会继续我的生活,但事实并非如此
让我编译。 我可以以某种方式抑制错误吗? 就像我说的,错误不是
在我的 Mac 上运行,程序运行良好。

整数和浮点数在内部使用不同的表示形式。 如果 int 和 float 中有相同的数字,则它们存储内的位模式完全不同。 如果您在应该传递浮点数时传递了整数,那么在任何情况下都不能期望代码能够正常工作。

此外,我断言您的 Mac 代码要么默默地使用该函数的重载版本(例如,您在该平台上使用 C++ 进行编译),要么您相信它可以工作,而实际上它是偶然工作的或实际上没有工作。

附录

迄今为止,没有任何编译器能够关闭错误。

警告意味着编译器认为您犯了一个错误

错误意味着编译器不知道该怎么做。

Regarding other answers here, it is not a good idea to tell the question author to turn off this warning. His code is broken - he's passing an unsigned int instead of a float. You should be telling him to fix his code!

This isn't a warning, it's an error. However, for me it's not an issue since my
program is only dealing with numbers that come out as integers, so I don't care that
they aren't floats. If it was just warnings I would move on with my life, but it's not
letting me compile. Can I suppress errors somehow? Like I said, the errors aren't
coming up on my Mac and the program is fine.

Integers and floats use different representations internally. If you have the same number in an int and a float, the bit pattern inside the storage for them is completely different. You cannot under any circumstances whatsoever expect your code to work if you are passing an integer when you should be passing a float.

Furthermore, I assert your Mac code either is silently using an overloaded version of that function (e.g. you are on that platform compiling with C++) or you believe it works when in fact it is working by chance or is not actually working.

Addendum

No compilers ever written has the ability to turn off errors.

A warning means the compiler thinks you're making a mistake.

An error means the compiler doesn't know what to do.

有几个选项:

在 C 中,解决方案是将整数强制转换为双精度数:

pow((double)i, (double)j)

在 C++ 中,您可以执行相同的操作,尽管您应该使用 C++ 风格的强制转换:

pow(static_cast<double>(i), static_cast<double>(j))

但更好的主意是使用重载 C++规定:

std::pow(static_cast<double>(i), j);

基数仍然必须是浮点值,但指数至少可以是 int

std:: 前缀可能不是必需的(大多数编译器也使该函数在全局命名空间中可用)。

当然,要访问该函数的 C++ 版本,您必须包含标头的 C++ 版本。

因此,您需要使用 #include

C++ 使用此命名约定提供每个 C 标头的 C++ 版本,而不是 #include。 如果 C 头文件名为 foo.h,则 C++ 版本将为 cfoo。 当您使用 C++ 编写时,您应该始终更喜欢这些版本。

There are a couple of options:

In C, the solution is simply to cast the ints to doubles:

pow((double)i, (double)j)

In C++, you can do the same, although you should use a C++-style cast:

pow(static_cast<double>(i), static_cast<double>(j))

But a better idea is to use the overload C++ provides:

std::pow(static_cast<double>(i), j);

The base still has to be a floating-point value, but the exponent can be an int at least

The std:: prefix probably isn't necessary (most compilers make the function available in the global namespace as well).

Of course, to access the C++ versions of the function, you have to include the C++ version of the header.

So instead of #include <math.h> you need to #include <cmath>

C++ provides C++ versions of every C header, using this naming convention. If the C header is called foo.h, the C++ version will be cfoo. When you're writing in C++, you should always prefer these versions.

靑春怀旧 2024-07-24 09:10:51

我不知道标志,但摆脱警告对我来说很容易。 只需双击“任务列表”中的每个警告并添加适当的转换,无论您愿意

(double) my_variable

还是

static_cast<double>(my_variable)

我猜测您是否收到不明确的警告,都在某处定义了多个 pow 函数。 无论如何,我认为最好明确一下。 就其价值而言,我投票支持 static_cast 选项。

I don't know of a flag, but getting rid of the warnings was easy enough for me. Just double click on each of the warnings in the "Task List" and add the appropriate casting, whether you prefer

(double) my_variable

or

static_cast<double>(my_variable)

I'm guessing if you're getting the ambiguous warning, there are multiple pow functions defined somewhere. It's better to be explicit in my opinion anyway. For what it's worth, my vote goes with the static_cast option.

瑾兮 2024-07-24 09:10:51

正如 Mehrdad 提到的,使用 #pragma warning 语法禁用警告。 文档位于此处 - http://msdn.microsoft.com/en-us/ library/2c8f766e.aspx

我倾向于修复警告而不是隐藏它们!

As Mehrdad mentioned, use the #pragma warning syntax to disable a warning. Documentation is here - http://msdn.microsoft.com/en-us/library/2c8f766e.aspx

I would be inclined to fix the warnings rather than hide them though!

一百个冬季 2024-07-24 09:10:51

C++ 对 int 指数的 pow/powf 进行了重载。 留意警告。

C++ has overloads for pow/powf for int exponents. Heed the warning.

厌味 2024-07-24 09:10:51

不要忽视这个或任何警告。 修复它们。 编译器是你的朋友,试图让你写出好的代码。 这是一个相信严厉的爱的朋友,但它是你的朋友。

如果您有一个无符号整型并且需要一个浮点数,请将您的无符号整数转换为浮点数。

MSDN Library 是 VC++ 实现的文档语言和 IDE 本身。

Don't ignore this or any warnings. Fix them. The compiler is your friend, trying to get you to write good code. It's a friend that believes in tough love, but it is your friend.

If you have an unsigned int and need a float, convert your unsigned in to a float.

And the MSDN Library is the documentation for both the VC++ implementation of the language and the IDE itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文