2016-04-25 39 views
0

用户输入的等级数。我正在使用动态数组分配。我对自己的代码有信心,但是Xcode在排序功能上给了我一个错误。我认为我做得对,但显然有些问题,我不完全确定它在哪里。我仍然试图找出动态内存分配,所以我相信这是我的错误产生的地方,我只是不知道原因在哪里。这是我的完整程序:Xcode中的错误:无法引用对重载函数的引用

// This program demonstrates the use of dynamic arrays 
#include <iostream> 
#include <algorithm> 
#include <iomanip> 
using namespace std; 

//Function Prototypes 
void sort(float *score[], int numOfScores); 

int main() 
{ 
float *scores; 
int total = 0; 
float average; 
float numOfScores; 
int count; 

cout << fixed << showpoint << setprecision(2); 

cout << "Enter the number of scores to be averaged and sorted."; 
cin >> numOfScores; 


scores = new float(numOfScores); 

for (count = 0; count < numOfScores; count++) 
{ 
    cout << "Please enter a score:" << endl; 
    cin >> scores[count];   } 

for (count = 0; count < numOfScores; count++) 
{ 
    total = total + scores[count]; 
} 

average = total/numOfScores; 

cout << "The average score is " << average << endl; 

sort(*scores, numOfScores); 

delete [] scores; 
return 0; 
} 

//******************************************* 
//    Sort Function 
// Bubble sort is used to sort the scores 
//******************************************* 
void sort(float *score[], int numOfScores) 
{ 
    do 
{ 
    bool swap = false; 
    for (int count = 0; count < (numOfScores -1); count++) 
    { 
     if (*score[count] > *score[count+1]) 
     { 
      float *temp = score[count]; 
      score[count] = score[count+1]; 
      score[count+1] = temp; 
      swap = true; 
     } 
    } 
}while(swap); //This is where I'm receiving the error. 
} 

谢谢!

+1

'分数=新的浮动(numOfScores);'我严重怀疑,做什么,你认为它。很确定你想要'scores = new float [numOfScores];'。 'float * score []'对于你的参数类型肯定是不正确的。你想'浮点数[]'。 – WhozCraig

+0

[在条件中使用在do-while循环中声明的变量]的可能重复(http://stackoverflow.com/questions/18541304/use-variables-declared-inside-do-while-loop-in-the-condition) – LogicStuff

+0

修复了该错误消息,但现在我收到了与“算法”相关的错误消息。我甚至试着拿出'#include ',我仍然收到错误信息。有任何想法吗? –

回答

0

swap位于do...while循环的本地,因此它不能在while条件中使用。人们会期望与该错误,但既然你有using namespace std;#include <algorithm>你现在已经推出了std::swap功能到程序

while(swap); 

的范围正试图std::swap转化为函数指针,但它不能作为它被重载并且不知道使用哪个超载。

有关为什么要避免使用using namespace std;参见更多阅读:Why is “using namespace std” in C++ considered bad practice?

相关问题