2014-12-04 86 views
-3

試圖寫在我使用罪程序中使用罪,但一直收到錯誤訊息試圖對C++接收錯誤消息

「罪

錯誤:重載函數的一個以上的實例」罪「匹配參數列表」

任何想法我做錯了什麼?

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

#define DEGREE 45 
#define THREE 3 

int 
    main(void) 
{ 
    double diagonal; 
    double length; 
    double volume; 
    double stands; 
    double volumeostands; 

//Get # of stands 
    printf("How many stands will you be making? \n"); 
    scanf("%d", &stands); 

//Get diagonal// 
    printf("What is the diagonal of the cube? \n"); 
    scanf("%d", &diagonal); 

//Find length 

    length = diagonal * sin(double(DEGREE); 

//Find volume 

    volume = 3(length); 
//Multiply volume by number of stands 

    volumeostands = volume * stands; 

//Display volume (for # of stands) 

    printf("Volume is %lf inches for %lf stands", &volumeostands, &stands); 


     return (0); 
} 
+2

你能後的代碼,以便我們可以幫助? – Poriferous 2014-12-04 02:44:21

+3

啊哈!所以問題是,「我的代碼是什麼?」。我猜測代碼中包含一個名爲「sin」的函數調用。 – 2014-12-04 02:49:28

+2

您發佈的代碼不會編譯,並且有許多其他錯誤,比如使用'%d'而不是'%f'讀取/打印雙精度數據,並將值的地址傳遞給'printf'時它應該是值。請發佈一些正確的代碼。 – 2014-12-04 04:36:10

回答

3

這時候,編譯器無法弄清楚重載函數去調用你的論點,你得到錯誤信息,它的通常做類型提升或轉換。例如,你可以有:

void fn(double d); 
void fn(float f); 

,如果你調用fn(x)其中x既不是float也不double但同樣可以成爲這些類型之一,編譯器將不知道該選哪。以下程序顯示了這樣的情景:

#include <iostream> 
int x(double d) { return 1; } 
int x(float f) { return 2; } 
int main(){ 
    std::cout << x(42) << '\n'; 
    return 0; 
} 

編譯與g++結果:

qq.cpp: In function ‘int main()’: 
qq.cpp:5:20: error: call of overloaded ‘x(int)’ is ambiguous 
    std::cout << x(42) << '\n'; 
        ^
qq.cpp:5:20: note: candidates are: 
qq.cpp:2:5: note: int x(double) 
int x(double d) { return 1; } 
    ^
qq.cpp:3:5: note: int x(float) 
int x(float f) { return 2; } 

因爲你傳遞的int值同樣可以成爲floatdouble,編譯器會抱怨。一個快速的解決辦法是把該類型的特定一個喜歡的東西:

std::cout << x((double)42) << '\n'; 

爲了您具體情況下,它可能正是我所展示的,在你打電話sin()與一個完整的類型。在C++ 11之前,唯一的重載是float,doublelong double。 C++ 11引入了整數類型的重載,它們將它們提升爲double,但如果你不是,則可以使用C++ 11的,只需使用上面顯示的轉換技巧即可。

如果(使用整型)的情況下,你應該意識到的第一件事是,sin()接受其參數爲弧度而不是度數,所以你幾乎肯定會希望使用浮點(圓周有360°,但只有2π弧度)。

-1

#include <stdio.h>

#include<iostream>

#include <math.h>

using namespace std;

#define DEGREE 45

#define THREE 3

int main(void) {

double diagonal; 
double length; 
double volume; 
double stands; 
double volumeostands; 

//Get # of stands 
cout << "How many stands will you be making? \n" ; 
cin >> stands; 

//Get diagonal// 
cout << "What is the diagonal of the cube? \n" ; 
cin >> diagonal; 

//Find length 

length = diagonal * sin(DEGREE); 

//Find volume 

volume = 3*length; 
//Multiply volume by number of stands 

volumeostands = volume * stands; 

//Display volume (for # of stands) 

cout << "Volume is " << volumeostands << " inches for stands " << stands << endl; 

system("pause"); 
return 0; 

}

+0

通常,答案會描述您所做的事情,並且格式正確。有趣的是,這段代碼更有可能產生最初問到的問題,即如果沒有缺少括號,問題中發佈的代碼將不會發布。 – 2014-12-04 06:33:20