Я новичок, и теперь я изучаю все о массивах и экспериментирую с различными способами его реализации. На этот раз мне очень хотелось узнать, как вернуть строковый массив в c ++ без использования вектора для исследовательских целей. Я попытался реализовать указатели как способ возврата массива строк, но он дает мне ошибку времени выполнения, заявляющую, что строковый индекс находится вне диапазона. Будь добр, посоветуй, если я ошибся, вернув массив строк, и предложи лучшие решения для этого.
Вот код Employee.h:
#pragma once
#include<string>
#include<iostream>
class Employee
{
private:
static const int recordSize = 100;
static const int fieldSize = 4;
std::string record[recordSize][fieldSize];
public:
Employee();
~Employee();
std::string * employeeReadData();
};
Вот это Employee.cpp
Employee::Employee()
{
}
std::string * Employee::employeeReadData() {
std::ifstream inFile;
inFile.open("C:\\Users\\RJ\\Desktop\\employee-info.txt");
static std::string recordCopy[recordSize][fieldSize];
for (int index = 0; index < recordSize; index++) {
for (int index2 = 0; index2 < fieldSize; index2++) {
inFile >> record[index][index2];
}
}
for (int index = 0; index < recordSize; index++) {
for (int index2 = 0; index2 < fieldSize; index2++) {
recordCopy[index][index2] = record[index][index2];
}
}
inFile.close();
std::string * point = * recordCopy;
return point;
}
Вот главная ():
int main()
{
Employee emp;
std::string* p = emp.employeeReadData();
cout << p[0][0] <<endl;
cout << p[0][1] << endl;
cout << p[0][2] << endl;
cout << p[0][3] << endl;
return 0;
}
работник-info.txt:
ID Firstname Lastname Sales
1 Reynard Drexler 150000.00
2 Joseph Bones 250000.00