C++:如何让动态数组的索引由文件中的行确定

标签 c++ arrays dynamic io

我正在尝试创建三个动态数组,它们的索引由文本文件中的行数决定。然后我需要使它们的索引值可修改。我在想全局数组是我最好的选择。

出于某种原因,我不断收到以下编译器错误: secondLab.obj:错误 LNK2019:未解析的外部符号“void __cdecl arrayInput(...)

问题,我该如何解决这个问题,基本达到我程序的目标。

这是我的代码:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <new>

using namespace std;

int INDEXES = 0;

string *names_Array = new string[INDEXES];
double *rates_Array = new double[INDEXES];
double *hours_Array = new double[INDEXES];

void subscript(ifstream&, int&, string&, double&, double&);
void arrayInput(istream&, string [], double [], double[],
     string&, double&, double&);

int main ()
{
    string names;
    double rates;
    double hours;

    string filename("employee sample file.txt");
    ifstream employeeInfo(filename.c_str());

    if (employeeInfo.fail())
    {
        cout << "Sorry, file was not successfully opened. "
             << "Please make sure your file exists and\n" 
             << "re-run the program." << endl;
    }   

    subscript(employeeInfo, INDEXES, names, rates, hours);

    arrayInput(employeeInfo, names_Array, rates_Array, hours_Array,
    names, rates, hours);

    cout << names_Array[0] << endl
         << names_Array[1] << endl
         << names_Array[2] << endl
         << names_Array[3] << endl
         << names_Array[4] << endl;

    delete[] names_Array;
    delete[] rates_Array;
    delete[] hours_Array;

    system("pause");
    return 0;
}

void subscript(ifstream& employeeInfo, int& INDEXES,
    string& names, double& rates, double& hours)
{
    while(!employeeInfo.eof())
    {   
        employeeInfo >> names >> rates >> hours;

        INDEXES++;
    }
}

void arrayInput(ifstream& employeeInfo, string names_Array[], 
    double rates_Array[], double hours_Array[], string& names, double& rates, double& hours)
{
    int i = 0;

    while(!employeeInfo.eof())
    {
        employeeInfo >> names >> rates >> hours;

        names_Array[i] = names;
        rates_Array[i] = rates;
        hours_Array[i] = hours;

        i++;
    }
}

最佳答案

arrayInput 的声明和定义不匹配,具体一个接受ifstream 参数,另一个接受istream。变化

void arrayInput(istream&, string [], double [], double[],
 string&, double&, double&);

void arrayInput(ifstream&, string [], double [], double[],
 string&, double&, double&);

关于C++:如何让动态数组的索引由文件中的行确定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13535382/

相关文章:

java - 创建其他类的数组。

c# - CsProj 迁移 : dynamic becoming object on Microsoft. Excel.Interop 程序集

objective-c - 动态类型、Objective-C,它是如何工作的?

c++ - 模板二元运算符重载决议

c++ - 线程安全持有人

c++ - 如何使这个非标准 union ISO C++ 兼容,同时保持使用的便利性?

c++ - 委托(delegate)给 removeAll() 的方法的常量指针参数

javascript - 比较数组 Javascript 中索引元素内的数字

arrays - Bash 数组脚本排除重复项

c++ - 使用动态内存的集合并集