【发布时间】:2017-02-12 16:46:47
【问题描述】:
对指针完全陌生,所以我为新手问题道歉。尝试调用我的函数时收到转换错误。 该函数应该返回一个指向更新后数组的指针,该数组末尾包含 1。
int* appendList(int x, int &arraySize, int *list)
{
arraySize++;
int *array2 = new int[arraySize];
for(int i=0; i < arraySize-1; i++)
{
array2[i] = list[i-1];
}
array2[arraySize]=x;
return array2;
}
我的主要功能如下。
int main()
{
int size=0;
int *list;
cout << "Please enter the size of your array: ";
cin >> size;
list = new int[size];
cout << "\nPlease enter the numbers in your list seperated by spaces: ";
for(int i=0; i < size; i++)
{
cin >> list[i];
}
cout << endl;
cout << "The array you entered is listed below\n ";
for(int i=0; i < size; i++)
{
cout << setw(3) << list[i];
}
list = appendList(1, size, list);
for(int i=0; i < size; i++)
{
cout << setw(3) << list[i];
}
return 0;
}
对函数 appendList 的调用导致参数 3 的转换错误,但我不确定为什么?函数参数必须保持原样。
感谢您的帮助。
【问题讨论】:
-
你使用
cout和setw而不使用std::,所以我猜你在上面声明了using namespace std。那么list变量名可能与std::list<T>发生冲突。尝试使用其他名称。 -
我还能用什么名字?
-
list1、my_list、ary 或与 c++ 关键字或 STL 模板不同的名称。或者您可以尝试不使用
using namespace std;来公开 std:: 中的所有名称。
标签: c++ arrays function pointers syntax