<fstream>命令的未定义引用

时间:2018-09-18 23:10:58

标签: c++

我有一个小问题。我的程序应该从文本文件中读取数据,然后将数据放入结构数组中,然后将数据输出到其他文本文件中。

我遇到的这个问题是,每当我进行编译时,都会出现以下错误:

  

对'printData(std :: basic_ofstream >&,CDdata *,int)'的未定义引用

现在,我知道什么是“未定义的引用”错误,但是我(以及对等方)对此进行了两次和三次检查,找不到我搞砸的地方。错误是指printData()中的main()行。我不知道那里有什么错误,但是getData()没有给我任何错误,并且该程序的早期版本(没有printData())编译得很好。

#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

1 个答案:

答案 0 :(得分:1)

您用于printData的原型与实现不匹配。在原型中,您可以通过值获取int值(无&),在实现中,您可以将其作为参考值(&)。那是两个不同的签名。

还请注意,如果您不打算更改值,则不应真正通过非const引用传递值。而且,即使通过const引用,也不应传递像int这样的原始值。参见:

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