如何返回多维数组?错误C2440
How to return a Multidimensional Array? Error C2440
我打开了一个.csv文件,并将其内容保存到一个二维数组中。当我试图返回它的值时,我得到了提到的错误。
如果我不使用函数或不返回数组,一切都会正常工作。最好的方法是什么?
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":无法从"std::string[300][30]'转换为"std:::basic_string,std::allocater>'">
您应该使用std::array而不是c样式数组来避免常见的初学者问题。不能将c样式数组传递给函数,也不能从函数返回c样式数组。数组衰减为一个指针,指针被传递。这个问题通过std::array:解决
在C++中,数组索引以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;
}
相关文章:
- 类动态数组错误
- 如何返回多维数组?错误C2440
- 数组错误(不允许类型不完整)
- C++ 将文件读取到数组错误:'operator>>'不匹配
- 不断增加的指针数组 错误:赋值中的类型不兼容
- 从文本文件读取 2D 数组错误
- 动态结构数组错误
- 旋转数组 - 错误
- 按字母顺序排序名称,数组错误
- 因为每个循环都读取数组错误
- 二维数组错误
- C 获取数组错误
- 编译时出现字符串数组错误
- c++2维指针类数组错误(使用openframework)
- C++数组错误
- C++上数组错误的大小
- C++数组错误:访问冲突读取位置0xC0000005
- c++2D数组错误
- C++:为“char [4]”数组错误获取太多初始值设定项
- 分段错误(核心转储)排序字符串数组错误