C++ “超过一个重载函数“std::pow”的实例与参数列表相匹配”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9554967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:58:52  来源:igfitidea点击:

"more than one instance of overloaded function "std::pow" matches the argument list"

c++namespacesconstantsc-preprocessor

提问by kiriloff

With C++, I try to

使用 C++,我尝试

#define TINY std::pow(10,-10)

I give the code with the #includeand namespace information for the class (.h) where TINY is defined

我给出了#include定义 TINY 的类 (.h) 和命名空间信息的代码

#pragma once
#include "MMath.h"
#include <string>
#include <cmath>
#include <vector>

using namespace std;

#define TINY std::pow(10,-10)

I use TINY in some function implementation in .cpp file, and TINY gives error

我在 .cpp 文件中的某些函数实现中使用了 TINY,而 TINY 给出了错误

IntelliSense: more than one instance of overloaded function "std::pow" matches the argument list

智能感知:超过一个重载函数“std::pow”的实例与参数列表匹配

What is right syntax?

什么是正确的语法?

回答by pmdj

Edit:I do agree with the commenters saying that using std::pow() in place of a literal constant is unnecessary - so for this particular problem, go with the 1.0E-10constant; my explanation of the actual error you were getting and the way to solve it still stands.

编辑:我同意评论者所说的使用 std::pow() 代替文字常量是不必要的 - 所以对于这个特定的问题,使用1.0E-10常量;我对您遇到的实际错误的解释以及解决它的方法仍然有效。

This has nothing to do with your #define. std::pow()is an overloaded function, and none of its overloads take (int, int)as arguments. You should provide arguments with types which unambiguously select an overload. Depending on the type of return value you want, you'll probably want to select one of these overloads:

这与您的#define. std::pow()是一个重载函数,它的重载都没有(int, int)作为参数。您应该提供具有明确选择重载的类型的参数。根据您想要的返回值类型,您可能需要选择以下重载之一:

      float pow (       float base,       float exponent );
     double pow (      double base,         int exponent );
long double pow ( long double base,         int exponent );

which you can invoke as follows:

您可以按如下方式调用:

std::pow(10.0f, -10.0f)
std::pow(10.0, -10)
std::pow(10.0L, -10)

respectively.

分别。

回答by Armen Tsirunyan

I think the best way is to define a constant variable and initialize it without using the powfunction, like this

我认为最好的方法是定义一个常量变量并在不使用pow函数的情况下对其进行初始化,就像这样

const double TINY = 1E-10; //1e-10 is 10 to the power of -10

回答by dasblinkenlight

Try using std::pow(10.0,-10.0)instead: std::powhas multiple overloadsmatching your argument list; specifying 10.0,-10.0forces the use of the specific overload:

尝试使用std::pow(10.0,-10.0)std::pow有多个重载匹配您的参数列表;指定10.0,-10.0强制使用特定的重载:

double pow(double base, double exponent);

Note that this #definemay be suboptimal depending on the use of TINY: every time you use it in your code, a call will be made to std::powto calculate the same value. A better approach would be to use a static variable, set it once, and use it from that point on.

请注意,这#define可能是次优的,具体取决于TINY: 每次在代码中使用它时,都会调用std::pow以计算相同的值。更好的方法是使用静态变量,设置一次,然后从那时起使用它。