简体   繁体   中英

problem passing array of struct to a function throwing undefined reference c++

I'm having issues with passing an array of structures to a function that searches them. I delcare an array of structs outside of main then copy it to a new array of structs inside of main (so I have access to them inside main and can pass them easier). Not sure why it is failing though. Can anyone help me?

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

const int MAX = 2000000;
const string DFile = "DFile.dms";
const string EFile = "EFile.dms";
const string VFile = "VFile.dms";

struct dogs
{
  int did;
  int age;
} DFBuffer[MAX];

struct examine
{
  int vid;
  int did;
  int fee;
} EFBuffer[MAX];

struct vet
{
  int vid;
  int eLevel;
} VFBuffer[MAX];

void readDF(ifstream&);
void readEF(ifstream&);
void readVF(ifstream&);
int getLineCount(ifstream&);
bool dogCompare(dogs lhs, dogs rhs) {return lhs.did < rhs.did;}
bool vetCompare(vet lhs, vet rhs) {return lhs.vid < rhs.vid;}
bool examCompare(examine lhs, examine rhs) {return lhs.vid < rhs.vid;}
void vetExamSeach(struct vet newVetArray[], struct examine newExamArray[], 
int, int);

int main()
{
  dogs * newDogArray = new dogs[MAX];
  examine * newExamArray = new examine[MAX];
  vet * newVetArray = new vet[MAX];

  ifstream DF, EF, VF;
  int dogCount = 0, examCount = 0, vetCount = 0;

  DF.open(DFile);
  readDF(DF);
  EF.open(EFile);
  readEF(EF);
  VF.open(VFile);
  readVF(VF);

  DF.open(DFile);
  dogCount = getLineCount(DF);
  EF.open(EFile);
  examCount = getLineCount(EF);
  VF.open(VFile);
  vetCount = getLineCount(VF);


  for(int i = 0; i < dogCount; i++)
    newDogArray[i] = DFBuffer[i];
  for(int i = 0; i < vetCount; i++)
    newVetArray[i] = VFBuffer[i];
  for(int i = 0; i < examCount; i++)
    newExamArray[i] = EFBuffer[i];

  cout << "Sorting...\n";
  sort(newDogArray, newDogArray + dogCount, dogCompare);
  sort(newExamArray, newExamArray + examCount, examCompare);
  sort(newVetArray, newVetArray + vetCount, vetCompare);
  cout << "Sorting complete!\n";

  vetExamSeach(newVetArray, newExamArray, vetCount, examCount);


  return 0;
}

here is the search function. for the sake of this question, im just trying to print what i pass it.

void search(vet newVetArray[], examine newExamArray[], int vCount, int eCount)
{
  for(int i = 1; i < vCount; i++)
    cout << "in search:     " << newVetArray[i].vid << ' ' << newVetArray[i].eLevel << endl;
}

here is the error I'm getting

Here is my files. Not asking you to do my HW just help me solve my issue

When, I run your code, I get the same compilation error of undefined reference for readDf, readEF, readVF, getLineCount and vetExamSeach. The error is because there is no definition of these functions. There are only just decalarations. When I define them (something random) the errors are gone. So, define the function(s) and the error(s) would be gone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM