我正在尝试将对struct的引用和ifstream的引用传递给函数。然后它将使用文件中的内容填充结构。如果我把它从功能中取出并将其放入主中则一切正常。但是现在我把它移到了我正在获取访问权限的函数中。我还是c ++的新手。所以任何帮助都会非常感激..
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
struct periodicElement
{
string code;
string name;
int number;
double weight;
};
void populateTable(periodicElement *aElement[], ifstream& inFile);
int main()
{
ifstream fileIn; // Input file identifier
const __int16 maxElements = 118;
int elements = 0;
periodicElement atomic[maxElements];
periodicElement *ptr = & atomic[maxElements];
string line;
if (fileIn.fail()) // Test for file existence
{
cout << "Problem opening file";
exit(-1);
}
populateTable(atomic, fileIn);
for (int i = 0; i < elements; i++)
{
cout << atomic[i]->code << endl
<< atomic[i]->name << endl
<< atomic[i]->number << endl
<< atomic[i]->weight << endl;
}
// Close file
fileIn.close();
system("pause");
return 0;
}
void populateTable(periodicElement *aElement[], ifstream& inFile)
{
int elements = 0;
while ((inFile >> aElement[elements]->code >> aElement[elements]->name >> aElement[elements]->number >> aElement[elements]->weight))
{
elements += 1;
}
}