gpt4 book ai didi

c++ - 将数组传递给函数

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:40 25 4
gpt4 key购买 nike

我目前正在学习 C++ 入门类(class),并且正在完成对数据进行排序的作业。我们最近介绍了结构,我决定使用结构来解决问题,而不是创建 3 个数组来保存数据文件中的信息。

我遇到的麻烦是当我试图将我的结构传递给我的函数时。这是我的错误:

analyze_data.cpp: In function ‘int main()’:
analyze_data.cpp:76: error: conversion from ‘weather*’ to non-scalar type ‘weather’ requested
analyze_data.cpp: In function ‘int find_pos_of_smallest(weather, int, int)’:
analyze_data.cpp:110: error: no match for ‘operator[]’ in ‘data[pos]’
analyze_data.cpp:110: error: no match for ‘operator[]’ in ‘data[pos_of_smallest]’

我不明白第 76 行的错误。我对将结构传递给函数做了一些研究,我发现在类型声明中添加了“&”。但是,我不知道它的作用或为什么我需要这样做,因为我们没有在类里面讲过它。我也试过了,但我得到了一组不同的错误。所以我想我不会发布那些,只是从我所知道的开始。

这是我的代码

  /*
Program name: Analyze data
Program discription: This program will read a data file named data.txt.
This data file is expected to be formated in a specific way and contain
specific weather information. The program will analyize this data and
return max, min temperatures, 'perfect days', how many cold fronts per
year, 10 coldest and hottest days in a year and finally find the 5
median days of the year.

Date: 10/1/2012
*/

#include <iostream>
#include <fstream>

using namespace std;

/*
* Declare struct's here
*
*/
struct weather
{
string date;
int high;
int low;
};

/*
* Forward declaration of a function. This declares the function,
* but does not define it. (Notice that there is no code, just
* a function header with a semicolon after it.
*
*/
int find_pos_of_smallest (weather data, int start_pos, int end_pos);

/* Our main function.
*
* Parameters:
* none
*
* Return value:
* 0 if we complete successfully, 1 if there was an error.
*/
int main()
{
//read the data file
ifstream weather_data("data.txt");
//declare array size, and then create array using struct
int days = 365;
weather data[days];

//store the data.txt in the array and then close the file
for (int i=0; i<days; i++)
{
weather_data >> data[i].date;
weather_data >> data[i].high;
weather_data >> data[i].low;
}
weather_data.close();

ofstream data_results ("results.txt");
// create the first 3 lines of the output reults in the following formated
data_results << "Assignment #5\n"
<< "CS 1410/2000\n"
<< "Jonathan Larsen\n";
/*
for(int i=0; i<3; i++)
{
cout<<data[i].date << " "<<data[i].high << " " << data[i].low<<endl;
}
cout<<endl;
*/
cout<<find_pos_of_smallest(data, 0, days)<<endl;


return 0; //no error so return a zero
}//end of program


/**** FUNCTIONS ****/

/* Write down exactly what the function will do (a postcondition).
* Write down what is required to use the function (any preconditions).
* Write down any other behavior or comments that will help a programmer.
*
* Parameters: (list parameters by type and name, and explain them)
* int example -- an example parameter
*
* Returns:
* double -- an example return value
*/

/* Returns the position of the smallest value found in the specified
* subarray. (Only the elements in the subarray
* between start_pos and end_pos inclusive are checked.)
*
* Parameters:
* d - a data array
* start_pos - the first position to check
* end_pos - the last position to check
*/
int find_pos_of_smallest (weather data, int start_pos, int end_pos)
{
int pos_of_smallest = start_pos;

for (int pos = start_pos+1; pos <= end_pos; pos++)
if (data[pos].low < data[pos_of_smallest].low)
pos_of_smallest = pos;

return pos_of_smallest;
}

最佳答案

您正在将一个数组传递给您的函数。您的函数需要采用 weather[]weather* 类型。例如:

int find_pos_of_smallest (weather* data, int start_pos, int end_pos)

您应该注意,即使您传递的不是数组而是常规结构,您仍然应该通过引用传递。要么传递指针 (weather*) 并使用取消引用 memeber 运算符 (->),要么通过引用传递 (weather&) 并使用普通成员运算符 (.)。这是因为按值传递(没有 */&)会导致将结构复制到堆栈上的新值中,这对于较大的值可能会花费大量时间(例如,如果字符串变大)。

关于c++ - 将数组传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12702063/

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