gpt4 book ai didi

c++ - 命令的 undefined reference

转载 作者:行者123 更新时间:2023-11-28 04:32:56 25 4
gpt4 key购买 nike

<分区>

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

我遇到的这个问题是,每当我去编译时,我都会收到以下错误:

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

现在,我明白什么是“ undefined reference ”错误,但是我(以及同行)对此进行了双重和三次检查,但找不到我搞砸的地方。错误是指 main() 中的 printData() 行。我不明白那里怎么会有错误,但是 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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com