在Visual Studio 2017中构建错误

问题描述 投票:-1回答:1

我尝试使用下面的代码实现sin(x)的近似,但编译器在运行程序时报告构建错误。我已经检查但不确定它有什么问题,可能是超载。非常感谢你的帮助!

/*Approximate sinx*/

#include <stdio.h>
#include <stdlib.h>
#include <cmath>
float sin(float);
float factor(int);
float times(float, int);

int main()
{
    float x;
    scanf_s("%f", &x);
    printf("%f", sin(x));
    system("cease");
    return 0;
}

float factor(int x)
{
    float factor=1;
    for (int i = 1; i <= x; i++)
    {
        factor *= i;
    }
    return factor;
}

float times(float x, int y)
{
    float times=1;
    for (int i = 0; i < y; i++)
    {
        times *= x;
    }
    return times;
}

float sin(float x)
{
    float sinx=0;
    int t = 1;
    for (int i = 1; i <= 1000; i++)
    {
        sinx +=t* times(x, 2*i-1) / factor(2*i-1);
        t *= -1;
    }

    return sinx;
}

输出如下:

1>------ Build started: Project: Testor, Configuration: Debug Win32 ------
1>Source.cpp
1>d:\ccode\testor\testor\source.cpp(6): error C2382: 'sin': redefinition; different exception specifications
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(281): note: see declaration of 'sin'
1>d:\ccode\testor\testor\source.cpp(14): error C2264: 'sin': error in function definition or declaration; function not called
1>d:\ccode\testor\testor\source.cpp(14): warning C4473: 'printf' : not enough arguments passed for format string
1>d:\ccode\testor\testor\source.cpp(14): note: placeholders and their parameters expect 1 variadic arguments, but 0 were provided
1>d:\ccode\testor\testor\source.cpp(14): note: the missing variadic argument 1 is required by format string '%f'
1>Done building project "Testor.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
c
1个回答
3
投票

啊,您正在尝试使用标准库中已存在的名称,为您的sinx提供不同的名称(或将其放在命名空间中)。 printf错误是由于编译器在sinx调用时遇到的问题。

© www.soinside.com 2019 - 2024. All rights reserved.