简体   繁体   English

C++; 将 std::array 作为 Function 参数传递

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

I know this is a common question, but how do I pass a std::array as a function parameter?我知道这是一个常见问题,但如何将std::array作为 function 参数传递? I've looked at answers for other questions on here asking the same thing, and the best "solution" I found was to use a template to set size_t to a keyword like SIZE .我在这里查看了其他问题的答案,询问同样的事情,我发现最好的“解决方案”是使用模板将size_t设置为SIZE之类的关键字。 I tried this, but it still gives me two compile time errors:我试过了,但它仍然给我两个编译时错误:

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

Both errors on the line where I have my void function definition.这两个错误都在我有void function 定义的那一行。

As far as I know, I did this exactly how the solution was typed out.据我所知,我正是按照解决方案的输入方式来做的。 I know I could instead pass through a std::vector , but I'm stubborn and want to learn this.我知道我可以改为通过std::vector ,但我很固执,想学习这个。

Here is my relevant code:这是我的相关代码:

#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.
}

I'd appreciate any help figuring out what it is that I'm doing wrong.如果能帮我弄清楚我做错了什么,我将不胜感激。

EDIT : I forgot to take out the second function parameter.编辑:我忘了取出第二个 function 参数。 I did that, but still the same issues.我这样做了,但仍然是同样的问题。

Your function definition still needs template parameters since the compiler has no way of knowing what SIZE is您的 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