Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对‘;的调用没有匹配函数;开始(int**&;)和#x2019; 我写了一个C++程序,作为FLLUE(3.43。CPP):_C++_Arrays_C++11_Compilation - Fatal编程技术网

对‘;的调用没有匹配函数;开始(int**&;)和#x2019; 我写了一个C++程序,作为FLLUE(3.43。CPP):

对‘;的调用没有匹配函数;开始(int**&;)和#x2019; 我写了一个C++程序,作为FLLUE(3.43。CPP):,c++,arrays,c++11,compilation,C++,Arrays,C++11,Compilation,我在谷歌上搜索,但没有找到类似的答案。指针和数组不一样。为了能够使用基于范围的,您的容器必须支持std::begin和std::end。可以使用标准的C数组,但不能使用指针。因为arr只是一个指针,所以无法推断它有多大。但是,由于您实际上要传入一个实数组,因此您可以在其维度上对函数进行模板化,以便通过引用获取实际数组,而不是将其衰减为指针: template <size_t X, size_t Y> void foo(const int (&arr)[X][Y]) {

我在谷歌上搜索,但没有找到类似的答案。

指针和数组不一样。为了能够使用基于范围的,您的容器必须支持
std::begin
std::end
。可以使用标准的C数组,但不能使用指针。

因为
arr
只是一个指针,所以无法推断它有多大。但是,由于您实际上要传入一个实数组,因此您可以在其维度上对函数进行模板化,以便通过引用获取实际数组,而不是将其衰减为指针:

template <size_t X, size_t Y>
void foo(const int (&arr)[X][Y])
{
    std::cout << "Dimensions are: " << X << "x" << Y << std::endl;

    for (const int (&row)[Y] : arr) {
        for (int val : row) {
            std::cout << val << ' ';
        }
        std::cout << std::endl;
    }
}

int main() {
    int arr[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
    foo(arr);
}
模板
void foo(常量int和arr)[X][Y])
{
std::cout
std::begin()
std::end()
不适用于指针。它们只适用于数组。如果要推断数组的大小,需要将其作为引用传递给函数:

#include <cstddef>
template <std::size_t A, std::size_t B>
void version_1(int (&arr)[B][A]) {
    for (const int (&p)[A] : arr) {
        for (int q : p) {
            cout << q << " ";
        }
        cout << '\n';
    }
}
#包括
模板
无效版本_1(内部和内部资源)[B][A]){
对于(常数整数和p)[A]:arr){
for(int q:p){

看不见。您对
version\u 1
的调用中也缺少aguments。请将参数类型更改为
const int(&arr)[3][4]
,并删除其他两个参数。基于范围的for循环只能用于
std::begin(x)
std::end(x)的对象
valid@quantdev我删除了本地程序中的aguments,但是为了删除这里的aguments,我已经更改了它。谢谢!为什么不使用一些合理的东西…比如向量?当然,循环变量的类型说明符可以替换为
auto&
@MattMcNabb是的,但是为了清晰起见,我想说明OP的类型基于e范围的for相当于从begin(容器)到end(容器)的迭代。请注意,不需要std::。函数begin和end需要defined@Shooter好的观点,实际上标准上说:“begin表达式和end表达式分别是begin(_范围)和end(_范围),其中begin()和end()函数使用参数相关查找(ADL)进行查找,ADL还包括std命名空间。“它包括
std
命名空间,可能是因为
std::begin
std::end
是为常规C样式数组定义的。
template <size_t X, size_t Y>
void foo(const int (&arr)[X][Y])
{
    std::cout << "Dimensions are: " << X << "x" << Y << std::endl;

    for (const int (&row)[Y] : arr) {
        for (int val : row) {
            std::cout << val << ' ';
        }
        std::cout << std::endl;
    }
}

int main() {
    int arr[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
    foo(arr);
}
#include <cstddef>
template <std::size_t A, std::size_t B>
void version_1(int (&arr)[B][A]) {
    for (const int (&p)[A] : arr) {
        for (int q : p) {
            cout << q << " ";
        }
        cout << '\n';
    }
}