-1

I've opened a .csv file and saved its contents to a 2-dimensional array. When I try to return its value I get the error mentioned.

Everything works if I don't use the function or if I don't return the array. What is the best way to do this?

 string read_csv()
 {
     std::ifstream file_csv("C:\\DRT\\Lista de Materiais\\Lista.csv");
     string csv_temp[600][40]

     while (std::getline(file_csv, temp)) 
     {
         j = 1;
         while (temp != "")
         {
             pos = temp.find(",");
             csv_temp[i][j] = temp.substr(0, pos);
             temp = temp.substr(pos + 1, string::npos);
             j = j + 1;
        }
        i = i + 1;
    }
    return csv_lista;
}

int main()
{
    string csv[600][30];
    csv = read_csv();
}

C2440: 'return': cannot convert from 'std::string [300][30]' to 'std::basic_string,std::allocator>'

Luís F.
  • 33
  • 3
  • 3
    Possible duplicate of [Return array in a function](https://stackoverflow.com/questions/3473438/return-array-in-a-function) – ChrisMM Oct 29 '19 at 15:51
  • 1
    First, your return type is only `string` not a multi-dimensional array of strings. Second, you cannot return c-style arrays in C++. – ChrisMM Oct 29 '19 at 15:51
  • Does this answer your question? [Returning multidimensional array from function](https://stackoverflow.com/questions/3716595/returning-multidimensional-array-from-function) –  Oct 29 '19 at 16:07
  • You also never declared ´´´csv_lista´´´ or assigned any values to it, if I am not mistaken. – Simon Oct 29 '19 at 16:58

1 Answers1

2

You should use std::array instead of c-style array to avoid usual beginner problems. You can't pass a c-style array to a function or return a c-style array from a function. The array decays to a pointer and the pointer is passed. This problem is solved with std::array:

In C++ array indexes start with 0.

#include <array>
#include <fstream>
#include <string>
using std::string;

using CSV = std::array<std::array<string, 30>, 600>;

CSV read_csv();

int main() {
    auto csv = read_csv();
}

CSV read_csv() {
    std::ifstream file_csv("Lista.csv");
    CSV csv_temp;

    std::string temp;
    for (std::size_t i{0}; i < csv_temp.size() && std::getline(file_csv, temp); ++i) {
        for (std::size_t j {0}; j < csv_temp[i].size() && temp != ""; ++j) {
            auto pos = temp.find(",");
            csv_temp[i][j] = temp.substr(0, pos);
            temp = temp.substr(pos + 1, string::npos);
        }
    }
    return csv_temp;
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • Note that `auto` doesn't work like that until C++14 or later. For C++11 and earlier, you will need to actually declare the return type as an array. –  Oct 29 '19 at 16:11
  • @Chipster We are in 2019. If someone uses C++11 or older instead of C++14 or C++17 he should mention it in the question and tag the question correspondingly. – Thomas Sablik Oct 29 '19 at 16:12
  • @ThomasSablik Hey, thanks, that fixed it. But now I wanted to do something else. Let's say I want the auto read_csv() to be in other file. If I do that, I get the "Identifier not found" error. How should I forward declare this function? Forward declaring it as "auto read_csv();" gives me the error: "'read_csv': a function that returns 'auto' cannot be used before it is defined" – Luís F. Oct 30 '19 at 09:16
  • @LuísF. You can't use auto in this case if you want to split declaration and definition. I fixed my answer – Thomas Sablik Oct 30 '19 at 09:45
  • @ThomasSablik It works thanks. but, I had to change: _using CSV = std::array, 30>;_ **to** _using CSV = std::array, 600>;_ – Luís F. Oct 30 '19 at 10:16
  • @LuísF. Ok, I changed it in my answer. – Thomas Sablik Oct 30 '19 at 11:38