从文件中读取双数据并使用函数

时间:2017-12-14 08:20:03

标签: c++ c++11

我想有一个函数,我可以传递任何大小的2D数组,它将被打印 - 听起来很简单。这应该有助于检查我的数学是否正确,因为我正在建立工程模型。

在显示的代码中,我首先从文本文件中读取数据,并将其存储在4x6双数组中,称为“数据”。然后我通过遍历每个元素并打印出来从main()打印数组,这很好。为了测试函数是否正常工作,我介绍了' data_test',另一个4x6数组,它分配了一些值。当我通过' data_test'对于该功能,它可以按需运行。但是当我传递数据时,它不起作用,从文件中读取的数据 - 我得到一个编译器错误说"没有匹配函数来调用'print2d_double(double [Nrow] [Ncol] )'"

这里发生了什么?当我传递一个我手动分配的值的数组时,为什么会编译,但是如果我尝试传递一个从文件中读入的数组,它就不会编译?我从文件中读取值的方式有问题吗?

我对c ++很陌生。谢谢你的任何想法! 文本文件如下所示:

1 1 0 0 0 100

2 1 0.01 0 0 200

3 1 0 0.01 0 300

4 1 0 0 0.01 400

#include <math.h>
#include <iostream>
#include <cmath>
#include <random>
#include <cstdio>
#include <fstream>
#include <typeinfo>
using namespace std;

// This prints a 2D array of doubles with any dimensions
template <size_t size_x, size_t size_y>
void print2d_double(double (&arr)[size_x][size_y])
{
    int counter = 0;
    int Nrow = size_x;
    int Ncol = size_y;
    for (int row=0;row<Nrow;row++){
        for (int col=0;col<Ncol;col++){
            printf("%f",arr[row][col]);
            counter++;
            if (counter % (Ncol) == 0)
            {
                printf("\n");
            }
            else
            {
                printf(", ");
            }
        }
    }
}

int main(int argc, char *argv[]) {

    int Nrow = 4; // number of rows to read in
    int Ncol = 6; // number of columns to read in
    double data[Nrow][Ncol]; // initialize array

    //Read data from the file
    ifstream file("fill_1_short.txt");
    if(file.is_open())
    {
        for (int row = 0; row<Nrow; row++){
            for(int col = 0; col < Ncol; col++)
            {
                file >> data[row][col];
            }
        }
    }
    else {printf("I'm closed - did not open file.\n");}

    //Printing 'data' directly in main() works
    printf("Printing 'data' from main():\n");
    int counter=0;
    for (int row=0;row<Nrow;row++){
        for (int col=0;col<Ncol;col++){
            printf("%f",data[row][col]);
            counter++;
            if (counter % (Ncol) == 0)
            {
                printf("\n");
            }
            else
            {
                printf(", ");
            }
        }
    }

    // Passing an array to the function also works, if the values of the array
    // are set directly:
    printf("Printing 'data_test' to test the function:\n");
    double data_test[4][6] = { {1,1,1,1,1,1},{2,2,2,2,2,2},{1,1,1,1,1,1},   {2,2,2,2,2,2} };
    print2d_double(data_test);

    // But passing the array 'data' that was read in does not work:
    printf("Printing 'data' with function:\n");
    print2d_double(data);

    return 0;
}

2 个答案:

答案 0 :(得分:0)

我认为问题在于编译器无法推断出data的大小。这是因为您使用非恒定值分配它。如果您将NrowNcol声明为常量,则会编译:

 int main(int argc, char *argv[]) {

    const int Nrow(4); // number of rows to read in
    const int Ncol(6); // number of columns to read in
    double data[Nrow][Ncol]; // initialize array
    ...
 }

通过这种方式,编译器在编译时知道数据的大小。

答案 1 :(得分:0)

double data[Nrow][Ncol];不符合C ++。由于NrowNcol未明确声明constexprdata是一个可变长度数组,其维度仅在运行时才知道。这是C11(不是C ++ 无论什么版本)中的可选功能,它被某些C ++编译器接受,例如gcc。

这就够了:

constexpr int Nrow = 4; // number of rows to read in
constexpr int Ncol = 6; // number of columns to read in

获取编译错误,并拥有正确的C ++代码。