c++ - 用于查找数组中信号幅度(最大值)的函数和指针

标签 c++

我的讲师在实时代码诊所中创建了这个函数,我试图将其复制出来,然后多次重写,直到我学习并理解该代码。

目前我不确定我需要在哪里定义“findgreatest”函数来让程序运行。我的印象是您必须在 main() 中定义函数。但是,可能还有更多我没有看到的错误。无论如何,将不胜感激一些有助于运行此代码并进行更详细解释的帮助。

谢谢

亚历克斯

#include <iostream>
#include <ctime>
using namespace std;

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

    const unsigned int size = 15;            // creates a const int for array
    int a_sig [size];                        // assigns int to array size

    for(int i=0; i<size; i++) {        
        a_sig[i] = rand() % 100;                 
        cout << *(a_sig+i) << endl;             
    }

    int findgreatest (int size, int a_sig) {  //"F deceleration not allowed"
        int max = -1;                           
        for (int i = 0; i < size; i++) {         
            if (*(a_sig+i) > max){
                max = *(a_sig+i);
            }
        }
        return max;
    };


    int maximum;

    maximum = findgreatest(size, a_sig);     //"undeclared identifier" 

    return 0;
};

最佳答案

Currently I'm unsure where I need to define the "findgreatest" function for the program to run. I was under the impression that you had to define functions within the main().

实际上,您不能在 main 中定义命名函数。您可以在错误 declaration not allowed 中看到这一点.

函数必须在使用前声明。在编译期间,编译器会记录函数定义的位置。当编译器看到这些函数之一被调用时,它知道要放置什么指令,因为它知道这些函数存在。但是,如果未声明(和定义)函数,则编译器无法解释函数调用。

您应该在 main 之前定义您的函数,或者在 main 之前声明您的函数并在 main 之后定义它。

选项:在main之前定义函数

如果你想在 main 之前定义函数,你的代码可能看起来像

#include <iostream>
#include <ctime>
#include <cstdlib>  // rand is defined here
using namespace std;

// Defines findgreatest
// findgreatest is now available for use later in program
int findgreatest (int size, int a_sig[]) // I added a `[]` in this signature
{
    int max = -1;                           
    for (int i = 0; i < size; i++) {         
        if (*(a_sig+i) > max){
            max = *(a_sig+i);
        }
    }
    return max;
}

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

    const unsigned int size = 15;            // creates a const int for array
    int a_sig [size];                        // assigns int to array size

    for(int i=0; i<size; i++) {        
        a_sig[i] = rand() % 100;                 
        cout << *(a_sig+i) << endl;             
    }

    int maximum;
    maximum = findgreatest(size, a_sig);

    // You probably want to do something with the maximum?
    cout << "\nMaximum is " << maximum << endl;

    return 0;
} // (I removed an unnecessary semicolon here)

选项:在main之前声明函数,在main之后定义

或者,您可以声明函数(即给出其名称和签名的描述)并在以后定义它。你可以这样做

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

// declares findgreatest
// The compiler knows that this function is defined somewhere and can make
// references to it. If the definition isn't also provided during compilation,
// an error is raised.
int findgreatest (int size, int a_sig[]);

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

    const unsigned int size = 15;            // creates a const int for array
    int a_sig [size];                        // assigns int to array size

    for(int i=0; i<size; i++) {        
        a_sig[i] = rand() % 100;                 
        cout << *(a_sig+i) << endl;             
    }

    int maximum;
    maximum = findgreatest(size, a_sig);

    // You probably want to do something with the maximum?
    cout << "\nMaximum is " << maximum << endl;

    return 0;
}
// Definition
int findgreatest (int size, int a_sig[])
{
    int max = -1;                           
    for (int i = 0; i < size; i++) {         
        if (*(a_sig+i) > max){
            max = *(a_sig+i);
        }
    }
    return max;
}

附加说明

  1. 使用rand() , 你需要 #include <cstdlib> .
  2. 您计算最大值但不对其进行任何操作。我在 main 的末尾添加了一条语句,将其输出到终端。
  3. 我删除了 main(){} 之后的分号.
  4. 您的函数签名 findgreatest(int size, int a_sig)不太对。第二个参数是一个数组,而不是一个整数。有几种不同的表示方式,但我将其更改为 findgreatest(int size, int a_sig[])向编译器表明它应该期待一个数组。

当你学习更多关于数组的知识时,你就会知道更多。您可能会重新访问表达式 *(a_sig + i) , 这有点奇怪。

关于c++ - 用于查找数组中信号幅度(最大值)的函数和指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55313918/

相关文章:

c++ - 一个简单的 C++ 函数,在不同的计算机上有不同的答案

c++ - iOS 处理强制应用程序退出的正确方法是什么

c++ - 矩阵 vector 乘积 CUDA 性能

c++函数参数char指针和字符串

c++ - 共享指针如何工作?

c++ - C volatile 位域结构的复制构造函数

c++ - 如何在cpp中以有效的方式比较 vector 和数组?

c++ - 具有中间变量的 Eigen 惰性评估

c++ - 苹果、橘子和指向最派生的 c++ 类的指针

C++通过多态和指针自定义异构容器