函数返回指向数组的指针
function returns pointer to array
我编写了这个 C++ 代码来使函数返回指向双精度数组的指针,这样我就将其用作 rvalue。我收到一条奇怪的错误消息,因为我无法理解它有什么问题。这是带有错误消息的代码
#include <iostream>
using std::cout;
using std::endl;
double* fct_returns_ptr(double, int); // function prototype
int main(void)
{
double test_array[] = { 3.0, 10.0, 1.5, 15.0 }; // test value
int len = (sizeof test_array)/(sizeof test_array[0]);
//double* ptr_result = new double(0.0); //[len] pointer to result
double* ptr_result = new double[len]; // (0.0) pointer to result
ptr_result = fct_returns_ptr(test_array, len);
for (int i=0; i<len; i++)
cout << endl << "Result = " << *(ptr_result+i); // display result
cout << endl;
delete [] ptr_result; // free the memory
return 0;
}
// function definition
double* fct_returns_ptr(double data[], int length)
{
double* result = new double(0.0);
for (int i=0; i<length; i++)
*(result+i) = 3.0*data[i];
return result;
}
/*
C:UserslaptopDesktopC_CPP>cl /Tp returns_ptr.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.40219.01 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
returns_ptr.cpp
c:Program Files (x86)Microsoft Visual Studio 10.0VCINCLUDExlocale(323) : warning C4530: C++
exception handler used, but unwind semantics are not enabled. Specify /EHsc
returns_ptr.cpp(13) : error C2664: 'returns_ptr' : cannot convert parameter 1 from 'double [4]' to 'double'
There is no context in which this conversion is possible
*/
在fct_returns_ptr()
中,行double* result = new double(0.0);
不会创建双精度数组,而是创建一个起始为 0.0 的双指针。 我怀疑你的意思是:
double* result = new double[length];
你也不需要
double* ptr_result = new double[len]; // (0.0) pointer to result
ptr_result = fct_returns_ptr(test_array, len);
在函数中创建数组时main()
。您可以将其更改为:
double* ptr_result = fct_returns_ptr(test_array, len);
#include <iostream>
using namespace std;
double* fct_returns_ptr(double *, int); // function prototype
int main(void)
{
double test_array[] = { 3.0, 10.0, 1.5, 15.0 }; // test value
int len = (sizeof test_array)/(sizeof test_array[0]);
//double* ptr_result = new double(0.0); //[len] pointer to result
double* ptr_result = new double[ len ] ;
ptr_result = fct_returns_ptr(test_array, len);
for (int i=0; i<len; i++)
cout << endl << "Result = " << *(ptr_result+i); // display result
cout << endl;
delete [] ptr_result; // free the memory
return 0;
}
// function definition
double* fct_returns_ptr(double *data, int length)
{
double* result = new double[length];
for (int i=0; i<length; i++)
*(result+i) = 3.0*data[i];
return result;
}
试试这个,有什么问题?您的函数原型和签名不匹配。
相关文章:
- 指向指向字符数组的指针数组的指针
- 通过指向指针数组的指针访问子类的属性
- C++,指针数组,指向双链表中的条目
- 在C++中,如何初始化指向wchar_t*的指针数组(生成wchar_t**)
- C++从函数指针数组调用函数
- 关于指向指针数组的指针
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 将链表转换为指针数组时出错
- C++ 对象指针数组的复制构造函数
- C++ - 循环访问指针数组会导致错误
- 删除指针数组 (C++) 中的元素
- 如何循环访问 cpp 中的函数返回的字符指针数组
- 将函数指针数组中的函数指针作为模板参数传递
- Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
- 具有推导参数的模板函数指针数组变量
- 如何模板化堆栈分配的多态指针数组到接口,包括派生类型的相应点?
- C++指针数组到字符数组中的特定位置
- 如何在C++中复制指针数组的数据
- 初始化类中的指针数组,并在另一个类中检索它
- 尽管直接设置了指针数组,但仍为空