“...重新声明为不同种类的符号”?

问题描述 投票:0回答:7
#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;
}

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

编辑:

#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;
}

^在戴上牙套之后,这仍然不起作用。它说“未定义引用‘WinMain@16’”...

c++ c for-loop syntax-error
7个回答
8
投票

您在函数内部重新定义了 low 和 high,这与参数中定义的冲突。

for 循环正在执行

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

你是故意的吗

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

但是我认为你想做

ans += delta_x*s;


3
投票

low
high
已作为
integrateF
方法的参数传递。但它们在方法内部再次被重新声明。因此出现错误。


1
投票

low 和 high 已作为IntegrateF 方法的参数传递,并且它们在方法内再次重新声明..

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


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



0
投票

你可能想尝试这样:-

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;
}

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

编辑:-

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

确保您已定义

main() or WinMain()
。另请检查 main() 是否未在您的命名空间内定义


0
投票

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

double integrateF = 0;

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

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

您甚至不必调用 main 内部的函数来尝试编译时出现错误,相反,编译器无法理解:

double integrateF = 0 = (double, double) { };
主要功能之外。


0
投票

在“双集成F(双低,双高)”中,您已经声明了低和高,然后在integraF内部您再次将它们声明为双,这就是您收到重新声明低和高错误的原因,您应该删除里面的声明并将

double integrateF(double low, double high)
更改为
double integrateF(double low = 0, double high = 0)


-1
投票

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

而不是

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

你应该这样做

double integrateF(double low, double high)
{
    low = 0;
    high = 20;
    .
    .
    .
}
© www.soinside.com 2019 - 2024. All rights reserved.