[英]Returning a Pointer from a function c++
當我嘗試在C ++中進行編譯時,它說存在對sort_Array(int *,int)的未定義引用。 我已經把這個弄糟了大約一個小時,但我不知道為什么會收到這個錯誤。 我以為是告訴我使用指針。 我必須完全按照為賦值編寫的原型使用原型,所以我無法像我以為它告訴我那樣將數組更改為指針或通過引用傳遞它。 我還必須從用戶那里獲取數組的大小。 可能是我需要動態分配數組的情況。 這讓我望而卻步,但我不確定。
這是原型
int *sort_Array(int [], int);
這就是我試圖使用它的方式
int size;
int initial_Array[size];
cout << "What is the size of your array?" << endl;
cin >> size;
cin.ignore();
sort_Array(initial_Array, size);
這是實際功能
int *sort_array(int unsorted_Array[], int size)
{
int *sorted_Array, i;
sorted_Array = new int[size];
//Function for sorting the array.
selection_Sort(unsorted_Array, size);
//Copies the newly sorted unsorted array variable to the sorted array varible.
for(i=0; i<size; i++)
{
unsorted_Array[i] = sorted_Array[i];
}
return sorted_Array;
}
您可以使用未初始化的size
定義intial_Array
:
int size;
int initial_Array[size];
哪個是UB。
但是你的錯誤來自調用sort_Array
當你定義sort_array
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.