簡體   English   中英

將結構數組傳遞給拋出未定義參考的函數的問題c ++

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

將結構數組傳遞給搜索它們的函數時遇到問題。 我在main外部delcare了一個結構體數組,然后將其復制到main內部的新結構體數組(因此我可以在main內部訪問它們,並且可以更輕松地傳遞它們)。 不知道為什么它會失敗。 誰能幫我?

#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;
}

這是搜索功能。 為了這個問題,我只是想打印我通過的內容。

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;
}

這是我得到的錯誤

這是我的文件。 不要求您做我的硬件只是幫助我解決我的問題

當我運行您的代碼時,對於readDf,readEF,readVF,getLineCount和vetExamSeach,我得到未定義引用的相同編譯錯誤。 該錯誤是因為沒有這些功能的定義。 只是貼花而已。 當我定義它們(某種隨機的)時,錯誤消失了。 因此,定義函數,錯誤將消失。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM