简体   繁体   English

将数组结构、ofstream 和整数传递给函数

[英]passing an array structure, ofstream, and interger to function

before I start I would like to apologize if the solution is easy.在我开始之前,如果解决方案很简单,我想道歉。 I am a beginner when it comes to coding.在编码方面,我是初学者。

When I try to compile my c++ code I get two error codes:当我尝试编译我的 C++ 代码时,我得到两个错误代码:

  1. undefined reference to 'outputFirstSheet(std::basic_ofstream&, int, Info*)'对“outputFirstSheet(std::basic_ofstream&, int, Info*)”的未定义引用
  2. error: 1d returned 1 exit status错误:1d 返回 1 个退出状态

This is my code:这是我的代码:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;




struct Info
{
    char hurricaneName[11];
    int ID;
    int life;
    int date;
    float avgWindSpeed;
    float avgRainFall;
    int totalTornadoes;
};




struct Tropicals
{
    int ID;
};

void outputFirstSheet(ofstream&, int counter, Info*);

int main()
{

    ifstream storms;
    storms.open("storms.txt");


    //This will count how many lines have information in them or are being used.
    int counter = 0;
    while (storms)
    {
        counter = counter + 1;
        storms.ignore(256,'\n');
    }
    //This is the end of that program

    Info names[counter];

    //Both pieces of code below will allow me to go back to the beginning of the storms.txt file
    storms.clear();
    storms.seekg(0, ios::beg);



    while (storms){
        storms.get(names[0].hurricaneName, 11);
        for(int a = 0; a < counter - 1 ; a++)
        {
            storms >> names[a].ID;
            storms >> names[a].life;
            storms >> names[a].date;
            float wind = 0.0;
            float sum = 0;
            for (int b = 0; b < 5;b++)
            {
                storms >> wind;
                sum = wind + sum;
            }
            names[a].avgWindSpeed = sum / 5;
            float rain = 0, total = 0;
            for (int c=0; c < 2; c++)
            {
                storms >> rain;
                total = rain + total;
            }
            names[a].avgRainFall = total / 2;
            storms >> names[a].totalTornadoes;

            //Making sure that I skip to the next line
            storms.ignore(256, '\n');
            storms.get(names[a+1].hurricaneName, 11);
        }

    }
    ofstream outfile;
    outfile.open("Results.txt");
    outfile << fixed << setprecision(2);


    // Printing out all the info in the STORM SUMMARY SHEET
    outputFirstSheet(outfile, counter, names);

    return 0;
}

void outputFirstSheet(ofstream &outfile, int counter, Info names)
{
    cout << "The program's results can be located in 'Results.txt'" << endl;
    outfile << setw(70) << "STORM SUMMARY SHEET" << endl;
    outfile << endl;
    outfile << "Name" << setw(10) << "ID" << setw(20) << " Life  " << setw(10) << "Date" << setw(20) << " Average  " << setw(20) << " Average " << setw(20) << "Tornadoes" << setw(20) << "Storm Level" << endl;
    outfile << "    " << setw(10) << "  " << setw(20) << "in days" << setw(10) << "Date" << setw(20) << "wind speed" << setw(20) << "rain fall" << setw(20) << " spawned " << setw(20) << "   level   " << endl;

    //Start adding the data to the STORM SUMMARY SHEET
    outfile << endl;
    for(int i = 0; i < counter - 1; i++)
    {
        outfile << names.hurricaneName[i];
    }




    outfile << endl << endl << "This code was created by Guillermo Molina Matus" << endl << endl;
}

The line that has the error is outputFirstSheet(outfile, counter, names);有错误的行是outputFirstSheet(outfile, counter, names); This function is supposed to be able to use ofstream, receive the counter information, and the Struct Info name variable but for some reason, I keep getting an error.这个函数应该能够使用 ofstream,接收计数器信息和 Struct Info 名称变量,但由于某种原因,我不断收到错误消息。

Could someone let me know what I did wrong?有人可以让我知道我做错了什么吗?

In the function definition use Info* names在函数定义中使用Info* 名称

Also, the arrays are passed using pointers so you really don't have to take a pointer for Info.此外,数组是使用指针传递的,因此您实际上不必为 Info 获取指针。 You can simply pass the Info array and receive it like you receive variables with value.您可以简单地传递 Info 数组并像接收带值的变量一样接收它。 It uses a pointer.它使用一个指针。 A new copy of array is not made.不制作数组的新副本。

void outputFirstSheet(ofstream &outfile, int counter, Info *names)
{
    cout << "The program's results can be located in 'Results.txt'" << endl;
    outfile << setw(70) << "STORM SUMMARY SHEET" << endl;
    outfile << endl;
    outfile << "Name" << setw(10) << "ID" << setw(20) << " Life  " << setw(10) << "Date" << setw(20) << " Average  " << setw(20) << " Average " << setw(20) << "Tornadoes" << setw(20) << "Storm Level" << endl;
    outfile << "    " << setw(10) << "  " << setw(20) << "in days" << setw(10) << "Date" << setw(20) << "wind speed" << setw(20) << "rain fall" << setw(20) << " spawned " << setw(20) << "   level   " << endl;

    //Start adding the data to the STORM SUMMARY SHEET
    outfile << endl;
    for(int i = 0; i < counter - 1; i++)
    {
        outfile << names->hurricaneName[i];
    }

    outfile << endl << endl << "This code was created by Guillermo Molina Matus" << endl << endl;
}

Your function definition should look like this.您的函数定义应如下所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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