簡體   English   中英

C++; 將 std::array 作為 Function 參數傳遞

[英]C++; Pass std::array as a Function Parameter

我知道這是一個常見問題,但如何將std::array作為 function 參數傳遞? 我在這里查看了其他問題的答案,詢問同樣的事情,我發現最好的“解決方案”是使用模板將size_t設置為SIZE之類的關鍵字。 我試過了,但它仍然給我兩個編譯時錯誤:

Error   C2065   'SIZE': undeclared identifier
Error   C2975   '_Size': invalid template argument for 'std::array', expected compile-time constant expression

這兩個錯誤都在我有void function 定義的那一行。

據我所知,我正是按照解決方案的輸入方式來做的。 我知道我可以改為通過std::vector ,但我很固執,想學習這個。

這是我的相關代碼:

#include <iostream>
#include <array>
using namespace std;

template<size_t SIZE>
void QuickSort(array<unsigned, SIZE>& arrayName);

int main()
{
    // main function.
}

void QuickSort(array<unsigned, SIZE>& arrayName)
{
    // whatever the function does.
}

如果能幫我弄清楚我做錯了什么,我將不勝感激。

編輯:我忘了取出第二個 function 參數。 我這樣做了,但仍然是同樣的問題。

您的 function 定義仍然需要模板參數,因為編譯器無法知道SIZE是什么

template<size_t SIZE>
// ^^^^^^^^^^^^^^^^^^
void QuickSort(array<unsigned, SIZE>& arrayName)
{
/// whatever the function does.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM