Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        去年关闭。
                                                                                            
                
        
我有一个小问题。我的程序应该从文本文件读取数据,然后将数据放入结构数组中,然后将数据输出到其他文本文件中。

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


  未定义对'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

最佳答案

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

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

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

关于c++ - <fstream>命令的 undefined reference ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52395762/

10-16 05:01