C++ “......重新声明为不同类型的符号”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19473753/
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 22:52:35  来源:igfitidea点击:

"...redeclared as different kind of symbol"?

c++cfor-loopsyntax-error

提问by CodeNewb

#include <stdio.h>
#include <math.h>

double integrateF(double low, double high)
{
    double low = 0;
    double high = 20;
    double delta_x=0;
    double x, ans;
    double s = 1/2*exp((-x*x)/2);

    for(x=low;x<=high;x++)
        delta_x = x+delta_x;
    ans = delta_x*s;

    return ans;
}

It says that low and high are "redeclared as different type of symbol" and I don't know what that means. Basically, all I'm doing here (READ: trying) is integrating from low (which I set to 0) to high (20) to find the Riemann sum. The for loop looks kinda trippy too...I'm so lost.

它说低和高被“重新声明为不同类型的符号”,我不知道这意味着什么。基本上,我在这里所做的一切(阅读:尝试)是从低(我设置为 0)到高(20)积分以找到黎曼和。for 循环看起来也有点迷幻……我很迷茫。

EDIT:

编辑:

#include <stdio.h>
#include <math.h>

double integrateF(double low, double high)
{
    low = 0;
    high = 20;
    double delta_x=0;
    double ans = 0;
    double x;
    double s = 1/2*exp((-x*x)/2);

    for(x=low;x<=high;x++)
    {
        delta_x = x+delta_x;
        ans = ans+(delta_x*s);
    }
    return ans;
}

^That still doesn't work, after the braces and all. It says "undefined reference to 'WinMain@16'"...

^在大括号和所有之后,这仍然不起作用。它说“对'WinMain@16'的未定义引用”...

回答by Martin Beckett

You are redefinign low and high inside the function which clash with those defined in the parameters.

您在函数内部重新定义了与参数中定义的那些冲突的低和高。

The for loop is doing

for 循环正在做

for(x=low;x<=high;x++)
{
   delta_x = x+delta_x;
}

did you mean it to do

你是故意的吗

for(x=low;x<=high;x++)
{
   delta_x = x+delta_x;
   ans = delta_x*s;
}

However I think you wanted to do ans += delta_x*s;

但是我认为你想做 ans += delta_x*s;

回答by Siddhartha Ghosh

lowand highare already passed as parameters of your integrateFmethod. But they are redeclared again inside the method. Hence the error.

low并且high已经作为integrateF方法的参数传递。但是它们在方法中再次被重新声明。因此错误。

回答by Devi K M

low and high are already passed as parameters of your integrateF method and they are redeclared again inside the method..

low 和 high 已经作为您的集成方法的参数传递,并且它们在方法中再次重新声明..

And x is not assigned a value when it is using for the calculation of s..

并且 x 在用于计算 s 时没有赋值。



double x, ans; double s = 1/2*exp((-x*x)/2);

双 x, ans; 双 s = 1/2*exp((-x*x)/2);



回答by Rahul Tripathi

You may want to try like this:-

你可能想像这样尝试:-

for(x=low;x<=high;x++)
{                          //Use brackets since you are redefining low and high inside the function
delta_x = x+delta_x;
ans = delta_x*s;
}

or

或者

for(x=low;x<=high;x++)
{                          //Use brackets since you are redefining low and high inside the function
delta_x = x+delta_x;
}

EDIT:-

编辑:-

It says "undefined reference to 'WinMain@16'"

它说“对'WinMain@16'的未定义引用”

Make sure you have main() or WinMain()defined. Also check that main() is not defined inside of your namespace

确保您已main() or WinMain()定义。还要检查 main() 是否未在您的命名空间内定义

回答by MassiveLegendHere

Another way you can cause this error is by "redefining" your function in a code where that nametag is already used as a variable outside of main function - Like so (psuedo code):

导致此错误的另一种方法是在代码中“重新定义”您的函数,其中名称标签已用作主函数之外的变量 - 像这样(伪代码):

double integrateF = 0;

main(){
 // get vars to integrate ...
}

double integrateF(double, double){
  //do integration
}

You don't even have to call the function inside of main to have an error trying to compile, instead, the compiler cannot make any sense of: double integrateF = 0 = (double, double) { };outside of main function.

您甚至不必在 main 内部调用函数来尝试编译时出错,相反,编译器无法理解: double integrateF = 0 = (double, double) { };在 main 函数之外。

回答by dmSherazi

when u have declared the data type in the Parameters, you don't have to re-declare them.

当您在参数中声明了数据类型时,您不必重新声明它们。

instead of

代替

double integrateF(double low, double high)
{
    double low = 0;
    double high = 20;
    .
    .
    .
}

you should do it like this

你应该这样做

double integrateF(double low, double high)
{
    low = 0;
    high = 20;
    .
    .
    .
}