c++ - 无法将 'double (*)[5]' 转换为 'double' 作为返回

标签 c++

我想知道为什么我总是报错并且无法返回一个数组;

此外,一旦 sell_item 函数实际工作并返回一个数组..我如何从主函数回显该数组。

谢谢

#include <iostream>
#include <fstream>

using namespace std;

ifstream infile;
ofstream outfile;

int itemnum = 3333;
string itemName="Cooking Range";
int Qauntity=1;

int NumberOfItems=2;
int NumberOfFields=5;

double function_Sell_Item(int   itemnum,string itemName, int Qauntity);

int main () {
    function_Sell_Item(itemnum, itemName, Qauntity);
}

double function_Sell_Item(int   itemnum,string itemName, int Qauntity) {
    double arraylist[2][5];

    for (int index =0; index < NumberOfItems; index++) {
        for (int i=0; i < NumberOfFields; i++) {
            arraylist[index][i]=0;
        }
    }

    return arraylist;
}
//// functions ends

: ;

最佳答案

你试图返回一个数组,这在 C++ 中是做不到的。

你应该考虑像 std::vector<double> 这样的东西,因为您可以返回。

typedef std::vector<double> MyVec;

MyVec foo() {
    MyVec v;
    v.push_back(3.142);
    v.push_back(2.718);
    return v;
}

int main() {
    MyVec z = foo();
    for (int i = 0; i < z.size(); i++) {
        std::cout << z[i] << "\n";
    }

    return 0;
}

关于c++ - 无法将 'double (*)[5]' 转换为 'double' 作为返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9897707/

相关文章:

c++ - 将结构推回结构中的 vector

c++ - 指向 auto_ptr 的指针而不是经典的双指针

linux - 什么是 Linux 等同于 MSVC++ 的选项/d1reportSingleClassLayout?

c++ - C++ 中的对象数组

c++ - 配置:错误:找不到MySQL包含目录

c++ - OpenCV HoughCircles仅检测1个圆

c++ - 为什么 std::vector::push_front() 不存在?

c++ - 从进程空间释放内存

c++ - 是否定义为 C++ 标准算法提供倒置范围?

c++ - 解析文件 I/O