0

我是编程新手,需要一些帮助,谢谢!

我有一个名为 namelist.txt 的文本文件。我需要将 inFile 传递给一个函数,该函数可以将名称列表(以 firstname lastname 的格式,例如:chuck norris)添加到一个名为 vecStudent 的向量中。您如何将 inFile 传递给函数,如果可能的话,我将如何将名称添加到向量中?

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

ifstream readtoVec(); //function prototypes
int displayAll();
int add();
int remove();
int savequit();

int main()
{
char cInput;
string strFileName;
vector<string> vecStudent;
ifstream inFile;
ofstream outFile;

{
    cout << "Please Enter the data file name (with location): ";
    cin >> strFileName;

    inFile.open(strFileName.c_str());

    if (inFile.fail())

    {
        cout << "Input file error!" << endl;

        return -1;
    }

    // call a function to read the contents of the input file into vecStudent
    else
    {
        readtoVec (ifstream& inFile);

我在这里收到一个名为“不允许类型名称”的错误

}



while (true)
{
    cout << "--------------------------------------" << endl;
    cout << " Student Record - Main Menu " << endl;
    cout << "--------------------------------------" << endl;
    cout << " Enter 1 to display ALL students " << endl;
    cout << " Enter 2 to add a student name " << endl;
    cout << " Enter 3 to delete a student name " << endl;
    cout << " Enter 4 to SAVE and quit the program " << endl;
    cout << "--------------------------------------" << endl;
    cout << " Enter menu option: " ;
    cin >> cInput;
    switch (cInput)
    {
        case '1':
            //call function
            break;
        case '2':
            //call function
            break;
        case '3':
            //call function
            break;
        case '4':
            //call function
            return 0;
        default:
            cout << "invalid input" << endl;
            break;
    }

return 0;
}
4

1 回答 1

1

ifstream readtoVec(); //函数原型

这表示readtoVec()返回一个ifstream,并且不接受任何参数,但在这里:

readtoVec (ifstream& inFile);

当你调用readtoVec. 至少根据您尝试调用它的方式,声明应该类似于void readtoVec(ifstream &);

当你调用它时,你没有指定参数的类型,所以调用看起来像:readtoVec(inFile);

于 2012-04-09T07:44:49.627 回答