【发布时间】:2020-02-25 17:57:23
【问题描述】:
我想看看数组和数组引用参数之间的区别,我得到了一个重新定义的错误和一个模棱两可的错误。我不明白为什么编译器不能告诉他们:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
typedef int arrTen[10];
void fun(int arr[]) {
cout << "arr[] called" << endl;
}
void fun(arrTen arr) {
cout << "arrTen called" << endl;
//cout << end(arr) - begin(arr) << endl;
}
void fun(arrTen &arr) {
cout << "arrTen reference called" << endl;
cout << end(arr) - begin(arr) << endl;
}
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
fun(arr); //ambiguous call to overloaded function
return 0;
}
错误信息:
Demo.cpp demo.cpp(24): error C2084: function 'void fun(int [])' already has a body demo.cpp(20): note: see previous definition of 'fun' demo.cpp(37): error C2668: 'fun': ambiguous call to overloaded function demo.cpp(29): note: could be 'void fun(arrTen (&))' demo.cpp(24): note: or 'void fun(int [])' demo.cpp(37): note: while trying to match the argument list '(int [10])'
似乎
fun(int arr[])和fun(arrTen arr)被重新定义了。 不知道为什么数组点参数等于数组参数。当我注释掉
fun(int arr[])时,fun(arr)是一个模棱两可的电话。 为什么编译器无法判断我已经传递了对fun的引用?
【问题讨论】:
标签: c++ arrays parameter-passing