警告传递`double'用于转换2的'void moveto(int,int)'

时间:2017-09-26 08:47:10

标签: c++

我有很多人喜欢这个节目: 无法弄清楚原因,请帮忙。 该程序工作,它显示我需要的图形,但我得到那些警告,从移动到lineto的行开始 这是完整的计划:

int main()
{

    int Um=220, f=5, R=5;
    double XL=10, XC=5, t, Im, Z, fi; // jaunie mainīgie
    int s=1000, n=0;  // jaunie mainīgie datu masīvam - s-max.izmērs, n-aktuāls datu skaits
    double ue[s],it[s],ur[s],ul[s],uc[s]; //datu masīvi

    Z=sqrt(R*R+pow(XL-XC,2));   //Z aprēķins
    Im = Um/Z;                  //Im aprēķins
    fi = asin((XL-XC)/Z);       //fi aprēķins

    //Ķēdes modelēšanas cikls
    for (t=0;t<=2;t+=0.002) //cikls mainot t no 0 lidz 2
    {
    ue[n] = Um*sin(2*M_PI*f*t);         //ue (avota spriegums) datu ieraksts masīvā
    it[n] = Im*sin(2*M_PI*f*t+fi);      //it (strāva) datu ieraksts masīvā
    ur[n] = Im*R*sin(2*M_PI*f*t+fi);    //ur (rez.spriegums) datu ieraksts masīvā
    ul[n] = Im*XL*sin(2*M_PI*f*t+fi+M_PI/2);    //ul (spol.spriegums) datu ieraksts masīvā
    uc[n] = Im*XC*sin(2*M_PI*f*t+fi-M_PI/2);    //uc (kond.spriegums) datu ieraksts masīvā  
    n++;        //mērījumu skaita palielināšana 
    }

    for (int i=0; i<5; i++) 
        cout << "i = " << i << " " << ue[i] << " " << it[i] << " " <<  ur[i] << " " << ul[i] << " " << uc[i] << endl;

    initwindow (1000,500,"Grafiks");    

    //Grafiku izvades cikls
    for (int x=0; x<n-1; x++)
    {
        //ue grafika izvades komandas
        setcolor(10);               //krāsa         
        moveto(x,50-ue[x]*0.1);     //kursora ievietošana
        lineto(x+1,50-ue[x+1]*0.1); //līnijas zīmēšana
        //ur grafika izvades komandas
        setcolor(11);
        moveto(x,150-ur[x]*0.1);
        lineto(x+1,150-ur[x+1]*0.1);
        //ul grafika izvades komandas
        setcolor(12);
        moveto(x,250-ul[x]*0.1);
        lineto(x+1,250-ul[x+1]*0.1);
        //uc grafika izvades komandas
        setcolor(13);
        moveto(x,350-uc[x]*0.1);
        lineto(x+1,350-uc[x+1]*0.1);        
        //it 4 grafiku izvades cikls
        for (int i=50; i<=350; i+=100)
        {
        //it grafika izvades komadas            
        setcolor(14);
        moveto(x,i-it[x]);
        lineto(x+1,i-it[x+1]);          
        }                               
    }   
    while (!kbhit());
    closegraph();
}

1 个答案:

答案 0 :(得分:0)

您正在将类型为double的参数传递给接受类型为int的参数的函数。一个简单的例子是:

#include <iostream>
void moveto(int x, int y){
    std::cout << x << ' ' << y << std::endl;
}
int main(){
    double a = 123.4, b = 567.8;
    moveto(a, b);  // decimal parts are cut off, warning issued
}

要禁止编译器警告,可以使用static_cast

将double转换为整数
moveto(static_cast<int>(a), static_cast<int>(b));

或者探索诸如std::round之类的舍入函数或修改函数,使其接受double类型的参数:

void moveto(double x, double y){
    std::cout << x << ' ' << y << std::endl;
}

将双精度数转换为整数会删除小数部分(不会发生舍入),因此123.4变为123567.8变为567