-1

尝试对数组进行函数调用,但错误:无法在没有参数列表的情况下引用模板数组不断出现。通过引用 main 函数来传递这个数组的最佳方法是什么?

using namespace std;

//function prototype
int readInput(vector<int> &vect);
void sort(vector<int> &vect, int &array, int &size);

int main()
{
vector<int> values;
int sum, avg;

sum = readInput(values);

sort(&array, &size); //cannot refer to template array without an argument list 

return 0;
}

int readInput(vector<int> &vect) 
{

int count;
int total = 0;

 ifstream inputFile("TopicFin.txt"); //open file

 if(!inputFile)
{
    return 0; // if file is not found, return 0
}

 while(inputFile >> count) //read file
 vect.push_back(count); //add to file

 for (int count = 0; count < vect.size(); count++)
 total+=vect[count]; //sum data in vector

return total;

}

void sort(vector<int> &vect, int array[], int &size)
{

for (int count = 0; count < vect.size(); count++)
   array[count] = vect[count];


int startScan, minIndex, minValue;

for(startScan = 0; startScan < (size-1); startScan++)
{
    minIndex = startScan;
    minValue = array[startScan];
    for(int index = startScan + 1; index < size; index++)
    {
        if (array[index] < minValue)
        {
            minValue = array[index];
            minIndex = index;
        }
    }

    array[minIndex] = array[startScan];
    array[startScan] = minValue;
}

for(int count = 0; count < size; count++)
    cout << array[count] << " " << endl;
}
4

1 回答 1

1

那是因为您正在调用sort(&array...,而没有array在 main 中声明,因此编译器假定您引用了std::array模板和扼流圈。您需要在代码中声明arraysize,并将第三个参数传递给sort.

于 2013-11-08T00:18:26.197 回答