C ++:尝试从自定义头文件调用函数时出现参数错误

时间:2014-05-10 20:47:36

标签: c++ arrays function header-files

因此,该程序的目的是拥有三个并行数组,其中包含十个帐户持有者,其ID和余额的名称。我的主文件如下所示:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include "IOFunctions.h" // My header file
using namespace std;

int main ()
{   
    const int AR_SIZE = 10;

    string nameAr;
    int    idAr;
    float  balanceAr;

    // F U N C T I O N -- ReadInData
    ReadInData(nameAr,
               idAr,
               balanceAr,
               AR_SIZE);
}

我得到的错误如下:http://i.imgur.com/1eHOZ7K.png

现在,头文件如下所示:

#ifndef IOFUNCTIONS_H_ // This is my own header
#define IOFUNCTIONS_H_

#include <iomanip>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


string ReadInData(string    nameArray[],
                  int       idArray[],
                  float     balanceArray[],
                  const int ARRAY_SIZE)
{
    ifstream inFile;

    string inFileName;
    string outFileName;

    // INPUT -- Prompts user for input file name
    cout << left << setw(40)
         << "What input file would you like to use? ";
    getline(cin, inFileName);

    // Checks that the file name entered is accessible
    while(inFileName != "InFile.txt")
    {
        cout << setw(40) << "Please enter a valid file name: ";
        getline(cin, inFileName);
    }

    // INPUT -- Prompts user for output file name
    cout << setw(40)
         << "What output file would you like to use? ";
    getline(cin, outFileName);

    // Checks that the file name entered is accurate to assignment
    while(outFileName != "OFile.txt")
    {
        cout << setw(40) << "Please enter a valid file name: ";
        getline(cin, outFileName);
    }

    // PROCESSING -- Takes the data from the input file and assigns it
    //               to the names array, ID array, and balance array

    // NAME ARRAY
    inFile.open(inFileName.c_str());
    for(int index = 0; index < ARRAY_SIZE; index++)
    {
        inFile >> nameArray[index];
    }
    inFile.close();

    // ID ARRAY
    inFile.open(inFileName.c_str());
    for(int index = 0; index < ARRAY_SIZE; index++)
    {
        inFile >> idArray[index];
    }
    inFile.close();

    // BALANCE ARRAY
    inFile.open(inFileName.c_str());
    for(int index = 0; index < ARRAY_SIZE; index++)
    {
        inFile >> balanceArray[index];
    }
    inFile.close();
    return outFileName;
}
#endif /* IOFUNCTIONS_H_ */

非常感谢所有帮助。如果我遗漏了什么,请告诉我。

1 个答案:

答案 0 :(得分:0)

ReadInData需要string*,但您传递string。通过传递引用来修复此问题:

string ReadInData(string    &nameArray, //<--
              int       idArray[],
              float     balanceArray[],
              const int ARRAY_SIZE)