0

I'm having a slight issue. My program is supposed to read data from a text file, put that data into an array of structs, and then output the data to a different text file, among other things.

This issue I'm having is that whenever I go to compile, I get the following error:

undefined reference to 'printData(std::basic_ofstream<char, std::char_traits<char> >&, CDdata*, int)'

Now, I understand what an "undefined reference" error is, however I (as well as a peer) have double and triple checked this and can't find where I screwed up. The error is referring to the printData() line in main(). I don't get how there's an error there, but getData() gave me no errors, and an earlier version of this program (without printData()) compiled just fine.

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

struct CDdata
{
  string title;
  float cost;
  int inventory;
};

void getData(ifstream& in, CDdata cdarray[], int& num);
void printData(ofstream& out, CDdata cdarray[], int num);

int main(int argc, char** argv){ //MAIN STARTS HERE

  ofstream outputFile;
  ifstream inputFile; // this and the line above it delcare the file objects

  CDdata cdarray[8]; //this creates the array of structs, as CDdata is the type

  inputFile.open("CDin.txt");// this and the line below open the in n out files
  outputFile.open("CDout.txt");

  int num = 0; //establishes num for use in getdata and printData

  int sum = 0; //establishes calcTotal for use below

  getData(inputFile, cdarray, num); //this establishes the getData command
  printData(outputFile, cdarray, num); //this establishes printdata and what can be used

  inputFile.close();
  outputFile.close();

  return 0;
}//end of main

//GET DATA STUFF
void getData(ifstream& in, CDdata cdarray[], int& num)//start of getdata function
{
  in >> num; //this reads the first line of the input file, which is 6 in our
            //case. this assigns it to num, with num being the amount of times
            //the below for loop will run for

  for(int k = 0; k<=num; k++)//this establishes k and the for loop
  {
    in >> cdarray[k].title;
    in >> cdarray[k].cost;
    in >> cdarray[k].inventory; //this and the above two lines read the data
                                //from the file and input it into the  array
  }
}//LAST BRACKET FOR GETDATA

//PRINT DATA STUFF

void printData(ofstream& out, CDdata cdarray[], int& num)//start of printData function
{
  out << "The following is the data from the structs:" << endl;
  for(int j = 0; j < num; j++)
  {
    out << cdarray[j].title;
    out << cdarray[j].cost;
    out << cdarray[j].inventory; //These three print just like the get one reads in
  }
}//LAST BRACKET FOR PRINT STUFF

int calcTotal(ofstream& out, CDdata cdarray[], int& num, int& sum) //establishes calcTotal function
{
  out << "This is the amount of CDs in inventory: " << endl;
  for(int h = 0; h<=num; h++)
  {                 //this loop calculates the sum of total CDs in inventory
    sum = sum + cdarray[h].inventory;
  }
  out << sum;
} //end calcTotal

int findbelow5(ofstream& out, CDdata cdarray[], int& num)
{
  out << "These are the names of the CDs with less than 5 copies in the inventory " << endl;
  for(int g = 0; g<=num; g++)
  {
    if(cdarray[g].inventory < 5)
      out << cdarray[g].title;
  }
}//end of findbelow5
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    Take a very careful look at your declaration of `printData` and its actual definition. There's a very tiny difference between the two. It's just like a game of "where's Waldo?", as soon as you find Waldo, you'll figure out how your compilation error happened. – Sam Varshavchik Sep 18 '18 at 23:16
  • If you cannot see the difference, you can always try to copy and paste line one above the other and **then it would be evident**. Or you can use a program that allows to compare text and copy both lines and let the program find the difference for you. **You could find your problems in seconds** instead of minutes asking a question and then wait an answer. – Phil1970 Sep 18 '18 at 23:57

1 Answers1

1

Your prototype for printData doesn't match the implementation. In the prototype you take an int by value (no &) and in the implementation you take it as a reference (&). That's two different signatures.

Note also that you shouldn't really pass values by non-const reference if you don't intend to change them. And you shouldn't pass primitive values like int even by const reference. See:

Is it better in C++ to pass by value or pass by constant reference?