声明原型时如何在主代码块中调用函数
How to call a function in the main block of code when a prototype is declared
我正在为类编写代码,该代码涉及与梯形规则和离散数据点进行数值积分。部分指令说要用原型调用函数:
double trapInt(const double xvals[], const double yvals[], int nElements);
我已经在代码的"int main"部分之前声明了原型,但我不完全理解调用函数的确切步骤。
注意:代码涉及使用一维数组。如果看到这会有所帮助,我也有作业的 PDF。
#include <iostream>
#include <ifstream>
using namespace std;
double trapInt(const double xvals[], const double yvals[], int nElements);
int main()
{
const int MAX_SIZE = 101;
double xData[MAX_SIZE];
double yData[MAX_SIZE];
ifstream infile("trapezoidData.txt");
if(infile.fail())
{
for(int a=0; a<MAX_SIZE; ++a)
{
infile >> xData[a] >> yData[a];
cout << xData[a] << 't' << yData[a] << endl;
}
}
else
{
cout << "Could not open infile." << endl;
}
cout.setf(ios::fixed);
cout.precision(3);
return 0;
}
调用函数非常简单。您要做的就是给出函数的名称,并放置参数,类似于如何声明原型:
main () {
// implementation...
trapInt(xvals, yvals, nElements);
// more implementation...
}
但请记住,仅将此行(或类似的行(添加到您的 main 函数中将无法在没有实现的情况下编译:
double trapInt(const double xvals[], const double yvals[], int nElements) {
// implementation of the function...
}
编辑:我原本想把函数的调用留给你一个练习,但如果你真的在黑暗中,我会给你举一个例子:
<块引用类>trapInt(xData, yData, MAX_SIZE(;
只需将该行添加到您的main()
中即可。
相关文章:
- 函数从唯一代码调用正确的子类方法
- 只允许授权代码调用库中的例程
- Android 无法从本机代码调用 Java 方法 JNI
- 如何从反应原生代码调用 C 函数?
- 如何在C++代码中使用 SVM Light?(可能无需从C++代码调用可执行文件)
- 如何从C代码调用PowerShell脚本
- 从 C 代码调用C++函数时出现问题
- 从 ObjC 代码调用 .mm 类方法时编译错误
- 代码调用反向函数不会在Ubuntu 18上的G 或Clang 上编译,但神秘地在Mac OSX上使用
- 如何使用C 代码调用MATLAB自定义函数
- 该代码调用副本或移动构造函数
- 无法在不同类上编译代码调用静态功能
- 如何从可从C++代码调用的 Ada 源代码构建静态库?
- 从 CPP 代码调用 Objective-C 方法
- 如何从C++代码调用 PL/pgSQL 函数
- C++如何判断static.lib是否有效,是否可从外部代码调用
- 当C代码调用Fortran子程序时,子程序顶部会出现分段错误
- JavaScript:从浏览器和 Node.js 中的 JavaScript 代码调用C++库
- C#到C代码P/调用多个std:字符串声明导致堆栈损坏
- 使用 Visual Studio 从C++代码调用程序集过程