2013-07-29 116 views
0

我早前向社區諮詢過這個程序,但我遇到了最後一個問題,並且在互聯網上進行了快速搜索,結果顯示它是常見的一個。數組類 - 錯誤:無法將參數從'double'轉換爲'int'

以下其次是我的構建輸出我的代碼

Grader.cpp

#include <iostream> 
#define MAXSIZE 100 
#include "Grader.h" 

Grader::Grader(){ 
my_Values [MAXSIZE] = 0; 
my_ValuesSeenSoFar = 0; 
} 

void Grader::addScore(int score){ 
if(my_ValuesSeenSoFar < MAXSIZE) 
    my_Values[my_ValuesSeenSoFar++] = score; 
} 
void Grader::addScores(int scores[], int size){ 
    for(int i=0; (i < size) && (my_ValuesSeenSoFar < MAXSIZE) ; i++) 
     my_Values[my_ValuesSeenSoFar++] = scores[i]; 
} 
void Grader::clear(){ 
    for(int i=0; i < MAXSIZE ; i++) 
     my_Values[i] = 0; 

} 

int Grader::findBiggest() const{ 

    int max = my_Values[0]; 
    for(int i=1; i < my_ValuesSeenSoFar ; i++) 
    { 
     if(my_Values[i] > max) 
      max = my_Values[i]; 
    } 
    return max; 
} 
int Grader::findSmallest() const{ 
    int min = my_Values[0]; 
    for(int i=1; i < my_ValuesSeenSoFar ; i++) 
    { 
     if(my_Values[i] < min) 
      min = my_Values[i]; 
    } 
    return min; 
} 

Driver.cpp

#include <iostream> 
#include "Grader.h" 
using namespace std; 
int main() 
{ 
Grader g; 
double d[5]= {99,70,85,93,84}; 
double e[4]= {100,81,60,91}; 

g.addScore(75); 
g.addScore(82); 
g.addScores(d, 5); 

cout << "Best Score = " << g.findBiggest() << endl; 
/// should give value 99 
cout << "Worst Score = " << g.findSmallest() << endl; 
/// should give value 70 
g.clear(); 

g.addScore(50); 
g.addScore(74); 
g.addScores(e, 4); 

cout << "Best Score = " << g.findBiggest() << endl; 
/// should give value 100 
cout << "Worst Score = " << g.findSmallest() << endl; 
/// should give value 50 
} 

而且我的錯誤輸出如下

------ Build started: Project: Grader, Configuration: Debug Win32 ------ Build started 7/29/2013 2:55:05 AM. InitializeBuildStatus: Touching "Debug\Grader.unsuccessfulbuild". ClCompile: Grader.cpp
Driver.cpp \psf\home\documents\visual studio 2010\projects\grader\grader\driver.cpp(12): error C2664: 'Grader::addScores' : cannot convert parameter 1 from 'double [5]' to 'int []' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast \psf\home\documents\visual studio 2010\projects\grader\grader\driver.cpp(22): error C2664: 'Grader::addScores' : cannot convert parameter 1 from 'double [4]' to 'int []' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Generating Code...

Build FAILED.

Time Elapsed 00:00:01.84 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

任何想法?

+0

你逝去的雙陣列,其期待的int數組功能。 – Jack

+0

@Jack我的印象是我無法更改driver.cpp代碼---但是,在仔細檢查下,項目沒有明確說這是禁止的 - 在我的假設下,顯然,我忽略了這個明顯的解決方案 – rezivor

+0

請喜歡/接受你認爲有用的答案。剛剛提到,因爲你是新來的。 – Jack

回答

0
double d[5]= {99,70,85,93,84}; 
double e[4]= {100,81,60,91}; 

d/e被定義爲雙然而,Grader::addScores只需要整型。

void Grader::addScores(int scores[], int size) 
g.addScores(e, 4); 

如果你不是說做d/e雙,tchange他們爲int類型:

int d[5]= {99,70,85,93,84}; 
int e[4]= {100,81,60,91}; 
0
double d[5]= {99,70,85,93,84}; 
double e[4]= {100,81,60,91}; 

使它成爲int。

+0

有沒有辦法做到這一點,無需更改代碼? – rezivor

+0

稱之爲g.addScore((int)d,5); –

+0

或者將Grader.cpp的函數的參數更改爲double .. –

相關問題