C++多种功能
Multiple functions in C++
今天我试图用多个函数做递归,我正在使用一些函数,因为我正在使用一个在其下方声明的函数
这是我的代码:
#include<bits/stdc++.h>
using namespace std;
#define MOD 10
int f(int x){
if(x == 4) return 1;
return 3*f(((2*x+2)%11)-1);
}
int q(int x){
if(x == 7) return 1;
return f(x) + q((3*x)%MOD);
}
int g(int x){
if(x == 0) return 1;
return (g(x-1)+f(g(x-1)) + f(x-1) + h(x-1))%MOD;
}
int h(int x){
if(x == 0) return 1;
return (2*h(x-1) + g(x-1) + q(f(x-1)%10))%MOD;
}
int main() {
cout << g(4);
return 0;
}
错误是在函数g(x)
中,它正在访问下面声明h(x)
并且h(x)
函数正在使用g(x)
函数,因此无法执行任何操作
请让我知道我应该怎么做才能完成这项工作。
多谢。
因此,您看到的问题是在您使用函数后定义的,因此编译器看不到它。这个"问题"是通过函数的声明来解决的,然后定义它。在您的情况下,您可以声明 main 上方的所有函数,然后在 main 之后定义(实现它(:
#include <iostream>
using namespace std;
#define MOD 10
// declaration ***
int f(int x);
int q(int x);
int g(int x);
int h(int x);
// **************
// main ***
int main() {
cout << g(4);
return 0;
}
// **************
// definition ***
int f(int x){
if(x == 4) return 1;
return 3*f(((2*x+2)%11)-1);
}
int q(int x){
if(x == 7) return 1;
return f(x) + q((3*x)%MOD);
}
int g(int x){
if(x == 0) return 1;
return (g(x-1)+f(g(x-1)) + f(x-1) + h(x-1))%MOD;
}
int h(int x){
if(x == 0) return 1;
return (2*h(x-1) + g(x-1) + q(f(x-1)%10))%MOD;
}
// **************
也不包括 #include 位/标准dc++.h
你需要添加一个函数 h 的前向声明才能编译你的代码,如下所示:
#define MOD 10
int f(int x){
if(x == 4) return 1;
return 3*f(((2*x+2)%11)-1);
}
int h(int); // add this line and you will be alright! ;)
int q(int x){
if(x == 7) return 1;
return f(x) + q((3*x)%MOD);
}
...
通常,您可以在此处找到为什么需要这样做。
首先声明你的函数。
#include<bits/stdc++.h>
#define MOD 10
// declaring your functions here makes sure that you can
// use them before they are fully defined.
int f(int x);
int q(int x);
int g(int x);
int h(int x);
// Now here below you can use the functions declared above
// at any place you wish.
int f(int x){
if(x == 4) return 1;
return 3*f(((2*x+2)%11)-1);
}
int q(int x){
if(x == 7) return 1;
return f(x) + q((3*x)%MOD);
}
int g(int x){
if(x == 0) return 1;
return (g(x-1)+f(g(x-1)) + f(x-1) + h(x-1))%MOD;
}
int h(int x){
if(x == 0) return 1;
return (2*h(x-1) + g(x-1) + q(f(x-1)%10))%MOD;
}
int main() {
std::cout << g(4);
return 0;
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 对可变参数使用声明.如何选择正确的功能
- 询问在设计我的手臂模拟器功能表示格式1
- 功能原型的目的
- 这里在 Linux 中具有"CreatePipe"和"CreateProcessW"功能吗?