简体   繁体   English

无法将指针数组传递给函数

[英]Trouble passing a pointer array to a function

In a program I am currently working on I have a template function included in a separate .h file that reads in five columns of data from a .txt file. 在当前正在使用的程序中,我有一个模板功能,该功能包含在单独的.h文件中,该文件从.txt文件中读取五列数据。 The data is passed to the main program and in this instance I only care about the array title "MISC_DATA". 数据被传递到主程序,在这种情况下,我只关心数组标题“ MISC_DATA”。 I am trying to determine the largest value in the array "MISC_DATA" and have written another function that the data has to be passed to, in order to determine this. 我试图确定数组“ MISC_DATA”中的最大值,并编写了另一个必须将数据传递给它的函数,以确定该值。 However, the compiler is telling me that it does not recognize the function call "Maximum_Value". 但是,编译器告诉我它无法识别函数调用“ Maximum_Value”。 I am pretty sure that it is having problems with the variable MISC_DATA included in the routine call and not the function itself. 我很确定它在例程调用中包含的变量MISC_DATA而不是函数本身存在问题。 Either it does not recognize MISC_DATA as an array or I have the syntax wrong. 它不能将MISC_DATA识别为数组,或者语法错误。 I'm only including the important snippets of code to make it more readable. 我只包含重要的代码片段以使其更具可读性。 The Read_Five_Columns functions works fine, it is the function "Maximum_Value", which is not being recognized by the compiler because of how the pointer array MISC_DATA is written in the main program. Read_Five_Columns函数可以正常工作,它是函数“ Maximum_Value”,由于在主程序中如何编写指针数组MISC_DATA,编译器无法识别该函数。 For clarification the variable MISC_DATA in the function call is a float which contains the array and the variable "size_Mis" is an integer which contains the array size. 为了澄清起见,函数调用中的变量MISC_DATA是一个包含数组的浮点数,而变量“ size_Mis”是一个包含数组大小的整数。 Any thoughts would be appreciated. 任何想法将不胜感激。

int main(int argc, const char * argv[]) {

#include "Use_RNG.h"
#include "Read_Columnar_File.h"
#include <fstream>
#include <iostream>

 std::vector<std::string> str3;
 std::vector<int> str4;
 std::vector<char> str5;
 std::vector<int> str6;

    unsigned long size_Mis;
    std::vector<float> MISC_DATA;  // Reads in Misc. spending data
    char File1[8];
    strcpy(File1, "Misc.txt");
    Read_Five_Columns(File1,MISC_DATA,str3,str4,str5,str6);
    str3.clear(); str4.clear(); str5.clear(); str6.clear();
    size_Mis = MISC_DATA.size();

float value;
value = Maximum_Value(MISC_DATA,size_Mis);

end_time = clock();
std::cout << std::endl << "Total Time: " << (end_time-start_time)/CLOCKS_PER_SEC << std::endl;
return 0;
}

int Maximum_Value(float *array,int array_size)
{
    float max = 0;
   for(int i =10; i < array_size-1; i++)
   {
        if(array[i] > max) max = array[i];
   }
    return max;
}

There are four problems I see here. 我在这里看到四个问题。

int main(int argc, const char * argv[]) {

#include "Use_RNG.h"
#include "Read_Columnar_File.h"
#include <fstream>
#include <iostream>

All of this stuff is in the wrong order. 所有这些东西的顺序都不正确。 You should not include system header files into function bodies, and typically you include standard library stuff before other stuff. 您不应将系统头文件包含在函数主体中,并且通常在其他内容之前包含标准库内容。 Fix it to read like this: 修复如下:

#include <fstream>
#include <iostream>

#include "Use_RNG.h"
#include "Read_Columnar_File.h"

int main(int argc, const char * argv[]) {

Secondly, you don't declare Maximum_Value before you use it. 其次,在使用前不要声明Maximum_Value You need to either move the definition of this function before the definition of main() or you need to add a prototype before main() : 您需要将该函数的定义移到main()的定义之前,或者需要在main()之前添加一个原型:

int Maximum_Value(float *array,int array_size);

int main(int argc, const char * argv[]) {

Then, you attempt to pass an std::vector<float> as a float* which does not work: 然后,您尝试将std::vector<float>传递为不起作用的float*

value = Maximum_Value(MISC_DATA,size_Mis);

However, because the storage for vectors is guaranteed to be contiguous and laid out like an array, you can pass a pointer to the first member safely: 但是,由于可以保证向量的存储是连续的并且像数组一样进行布局,因此可以安全地将指针传递给第一个成员:

value = Maximum_Value(&MISC_DATA[0],size_Mis);

Finally, you return int from Maximum_Value when you should probably be returning float . 最后,当您可能应该返回float时,将从Maximum_Value返回int


If possible I would suggest leveraging std::max_element , which is part of the standard <algorithm> header: 如果可能的话,我建议利用std::max_element ,它是标准<algorithm>标头的一部分:

// If you don't have C++11 then use std::vector<float>::iterator instead of auto.
auto max = std::max_element(MISC_DATA.begin(), MISC_DATA.end());

Now max is an iterator to the largest element, so *max would be the largest float itself. 现在max是最大元素的迭代器,因此*max本身就是最大float

(If the input range was empty, then max will be equal to MISC_DATA.end() , so the equivalent to your function would be value = max == MISC_DATA.end() ? 0f : *max; .) (如果输入范围为空,则max等于MISC_DATA.end() ,因此等效于您的函数的value = max == MISC_DATA.end() ? 0f : *max;

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

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