簡體   English   中英

警告傳遞“double”以轉換“void moveto(int, int)”的 2

[英]Warning passing `double' for converting 2 of `void moveto(int, int)'

我在這個程序中遇到了很多問題:不知道為什么,請幫忙。 該程序有效,它顯示了我需要的圖形,但我收到了以 moveto 和 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();
}

您將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將雙精度轉換為整數:

moveto(static_cast<int>(a), static_cast<int>(b));

或者探索舍入函數,例如std::round或修改函數,使其接受double類型的參數:

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

將 doubles 轉換為 integer 會刪除小數部分(不發生四舍五入),因此123.4變為123567.8變為567

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM