-1

I am getting an undefined reference error. I've stared at this code for about an hour trying to figure it out. This is the error I get.

undefined reference to fileToArray(std::basic_fstream<char, std::char_traits<char> >&, int*, int)

This is my code.

#include <iostream> 
#include <fstream>
#include <string>
#include <cctype>
#include <cstring>

using namespace std;

void arrayToFile(fstream&, int*, int);
void fileToArray(fstream&, int*, int);

int main()
{
fstream dataFile;
int numInt; //Size of array
int* numArray; //Array to hold integers
int num; //Integer for user to input into array.

cout << "Enter the number of integers you would like to put inside of an array: ";
cin >> numInt;

numArray = new int[numInt];

cout << "\nFill in the array with integers";
for (int i = 0; i < numInt; i++)
{
    cout << "Enter integer for position " << i << ": ";
    cin >> num;
    numArray[i] = num;
}

arrayToFile(dataFile, numArray, numInt);

fileToArray(dataFile, numArray, numInt);

cout << "Contents of numArray" << endl;
for (int i = 0; i < numInt; i++)
{
    cout << numArray[i] << " ";
}

delete [] numArray;

return 0;
}

//****************************
//Define arrayToFile function*
//****************************
void arrayToFile(fstream &dataFile, int* numArray, int SIZE)
{
dataFile.open("Ch12p8.dat", ios::out | ios::binary);

if(dataFile)
    dataFile.write((char*)numArray, SIZE);
dataFile.close();
}//end arrayToFile function

//****************************
//Define fileToArray function*
//****************************
void fileToFile(fstream &dataFile, int* numArray, int SIZE)
{

dataFile.open("Ch12p8.dat", ios::in | ios::binary);
if(dataFile)
    dataFile.read((char*)numArray, SIZE);
dataFile.close();
}//end fileToArray function
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268

3 Answers3

1

Instead of function fileToArray you defined function fileToFile. I think it is a typo.

//****************************
//Define fileToArray function*
//****************************
void fileToFile(fstream &dataFile, int* numArray, int SIZE)
     ^^^^^^^^^^
{

dataFile.open("Ch12p8.dat", ios::in | ios::binary);
if(dataFile)
    dataFile.read((char*)numArray, SIZE);
dataFile.close();
}//end fileToArray function
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0
void fileToFile

That's why. You declared fileToArray, but never defined it.

cf-
  • 8,598
  • 9
  • 36
  • 58
0

You have a typo in the definition of fileToArray

void fileToFile(fstream &dataFile, int* numArray, int SIZE)

You called it fileToFile instead.

harmic
  • 28,606
  • 5
  • 67
  • 91