c++:尝试从自定义头文件调用函数时,参数错误
C++: Argument error while trying to call a function from a custom header file
因此,这个程序意味着有三个并行数组,它们包含十个帐户持有人的姓名、他们的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);
}
我得到的错误看起来像这样:https://i.stack.imgur.com/TuNh7.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_ */
非常感谢所有的帮助。如果我遗漏了什么,请告诉我
ReadInData
接受string*
,但您传递的是string
。通过传递一个引用来修复这个问题:
string ReadInData(string &nameArray, //<--
int idArray[],
float balanceArray[],
const int ARRAY_SIZE)
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销