All I want is to store multiple char arrays as member data in the private field of my FileIO class. For some reason I can have one char array and it works fine, but I soon as I add a second, I get an the error stated in the title.
This code works fine:
class FileIO
{
private:
char accNum[];
public:
FileIO();
~FileIO();
void WriteData(Account*);
void WriteData(Person*);
void ReadData(Account*);
};
This code throws an error:
class FileIO
{
private:
char accNum[];
char persName[];
public:
FileIO();
~FileIO();
void WriteData(Account*);
void WriteData(Person*);
void ReadData(Account*);
};
accNum[] is being used in the ReadData(Account*) function to store one character retrieved from a text file using getline(). Here's the code for that:
void FileIO::ReadData(Account * acc)
{
ifstream accFile("accInfo.txt");
accFile.getline(accNum, 100);
cout << accNum << "\n";
accFile.close();
}
There are more lines in the same text file that I want to store in separate char arrays, but as you can see, I can apparently only have one array as a member variable of the FileIO class. Why is this?
char accNum[];
is a zero sized array and is illegal in C++.
If you are going to be dealing with "strings" then you should scrap using c-style strings and use a std::string
. Using a std::string
your code would then become
class FileIO
{
private:
std::string accNum;
public:
FileIO();
~FileIO();
void WriteData(Account*);
void WriteData(Person*);
void ReadData(Account*);
};
void FileIO::ReadData(Account * acc)
{
ifstream accFile("accInfo.txt");
getline(accFile, accNum);
cout << accNum << "\n";
accFile.close();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.