函数调用引用数组

时间:2013-11-08 00:13:26

标签: c++ arrays function reference

尝试对数组执行函数调用,但错误:无法引用模板数组而不会出现参数列表。通过引用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;
}

1 个答案:

答案 0 :(得分:1)

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