c++中的函数声明
Declaration of function in c++
我写了这段代码,但我不知道如何更改它。我想在我的 Raspberry pi 中使用此代码与声纳 - hc sr 04 来测量距离。请问你知道如何修复我的代码吗? :) 在此之前我写了一个代码,例如。这是我的真实代码。所以请再次检查:) 谢谢!
int zmeratSonar1() {
int smer = 0;
printf("meram sonar1");
digitalWrite(TRIGsonar1, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar1, LOW);
while (digitalRead(ECHOsonar1)==LOW);
long zaciatok = micros();
while (digitalRead(ECHOsonar1)==HIGH);
long cas = micros() - zaciatok;
int vzdialenost = cas/58;
if(vzdialenost < 100) {
smer = zmeratSonar28(); // <----here is my problem
}
else if(vzdialenost > 100) {
zmeratSonar1();
}
return smer;
}
int zmeratSonar28(){
int smer = 0;
printf("meram sonare 2 a 8");
//------------SONAR 2---------------------
digitalWrite(TRIGsonar2, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar2, LOW);
while (digitalRead(ECHOsonar2)==LOW);
long startTime2 = micros();
while (digitalRead(ECHOsonar2)==HIGH);
long travelTime2 = micros() - startTime2;
int distance2 = travelTime2/58;
//------------SONAR 8----------------------
digitalWrite(TRIGsonar8, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar8, LOW);
while (digitalRead(ECHOsonar8)==LOW);
long startTime8 = micros();
while (digitalRead(ECHOsonar8)==HIGH);
long travelTime8 = micros() - startTime8;
int distance8 = travelTime8/58;
//porovnanie vzdialenosti
if(distance2 > 100 || distance8 > 100) {
if(distance2 > distance8) {
smer = 2;
}
else {
smer = 8;
}
}
else{
smer = 0;
}
return smer;
}
您在声明函数 sum
之前正在使用它。您可以将函数 sum 移到 use 之上,也可以向前声明它:
int sum(); // Forward declared
int number = 0;
int a() {
for(int i = 0; i < 20; i++) {
if((i % 2) == 1) {
number = sum();
}
}
return number;
}
int sum() {
number = number + 100;
return number;
}
这个问题的进一步解释,可以在Stack Overflow的其他地方找到,比如这个问题的答案:Why do functions need to be declared before they are used?
注意:我从来没有彻底测试过你的代码,我想你也从来没有做过,所以正如 LogicStuff 指出的那样,它甚至没有编译,我做了一些更改以使代码编译,因为很少更改尽可能使原始代码仍然可见。感谢您指出 LogicStuff。
我想你的问题是编译错误。您在 sum
尚不可见的地方使用它。要么将总和移动到 a() 之上,要么在 a()
.
之上使用 int sum();
向前声明它
另一个问题是:
if(i % 2 = 1) {
应该是:
if((i % 2) == 1) {
^~~~~ !!
哇,我发现了第三个问题:)
您尝试在 int sum()
中使用变量 number
,而该变量是在 a()
中声明的,您根本做不到。您应该通过引用将数字传递给求和(不需要 return 值,您 return 它在数字参数中):
void sum(int& number) {
并称它为:
sum(number); // this is in place of `number = sum();`
在a()
您的代码中存在许多会导致编译器错误的错误:
如果您在 int a()
中使用 sum()
函数,则需要前向声明它。
int sum();
int add() {
//
}
int sum() {
//
}
所有 sum()
然而,真正做的是将 100 添加到变量中。这可以很容易地通过 +=
运算符合并到 add()
函数中,这意味着您的代码等同于:
int a() {
int number = 0; //Added a necessary ';'
for(int i = 0; i < 20; i++) {
if(i % 2 == 1) { //Corrected this from if(i % 2 = 1)
number += 100; //No need for the sum() function
}
}
return number;
}
关键点:
您可以对您的设置执行 很多 的额外优化;本质上,int a()
可以简化为
int a() {
return 1000;
}
所以,您可以很容易地不让a()
成为一个函数:
#define a() 1000
或者,(可能更好):
const int a = 1000;
编辑:
对于更新后的代码,您需要在 int zmeratSonar1()
开始之前编写 int zmeratSonar28();
:
int zmeratSonar28();
int zmeratSonar1() {
//Sonar code stuff
}
int zmeratSonar28() {
//Other sonar code stuff
}
我写了这段代码,但我不知道如何更改它。我想在我的 Raspberry pi 中使用此代码与声纳 - hc sr 04 来测量距离。请问你知道如何修复我的代码吗? :) 在此之前我写了一个代码,例如。这是我的真实代码。所以请再次检查:) 谢谢!
int zmeratSonar1() {
int smer = 0;
printf("meram sonar1");
digitalWrite(TRIGsonar1, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar1, LOW);
while (digitalRead(ECHOsonar1)==LOW);
long zaciatok = micros();
while (digitalRead(ECHOsonar1)==HIGH);
long cas = micros() - zaciatok;
int vzdialenost = cas/58;
if(vzdialenost < 100) {
smer = zmeratSonar28(); // <----here is my problem
}
else if(vzdialenost > 100) {
zmeratSonar1();
}
return smer;
}
int zmeratSonar28(){
int smer = 0;
printf("meram sonare 2 a 8");
//------------SONAR 2---------------------
digitalWrite(TRIGsonar2, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar2, LOW);
while (digitalRead(ECHOsonar2)==LOW);
long startTime2 = micros();
while (digitalRead(ECHOsonar2)==HIGH);
long travelTime2 = micros() - startTime2;
int distance2 = travelTime2/58;
//------------SONAR 8----------------------
digitalWrite(TRIGsonar8, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar8, LOW);
while (digitalRead(ECHOsonar8)==LOW);
long startTime8 = micros();
while (digitalRead(ECHOsonar8)==HIGH);
long travelTime8 = micros() - startTime8;
int distance8 = travelTime8/58;
//porovnanie vzdialenosti
if(distance2 > 100 || distance8 > 100) {
if(distance2 > distance8) {
smer = 2;
}
else {
smer = 8;
}
}
else{
smer = 0;
}
return smer;
}
您在声明函数 sum
之前正在使用它。您可以将函数 sum 移到 use 之上,也可以向前声明它:
int sum(); // Forward declared
int number = 0;
int a() {
for(int i = 0; i < 20; i++) {
if((i % 2) == 1) {
number = sum();
}
}
return number;
}
int sum() {
number = number + 100;
return number;
}
这个问题的进一步解释,可以在Stack Overflow的其他地方找到,比如这个问题的答案:Why do functions need to be declared before they are used?
注意:我从来没有彻底测试过你的代码,我想你也从来没有做过,所以正如 LogicStuff 指出的那样,它甚至没有编译,我做了一些更改以使代码编译,因为很少更改尽可能使原始代码仍然可见。感谢您指出 LogicStuff。
我想你的问题是编译错误。您在 sum
尚不可见的地方使用它。要么将总和移动到 a() 之上,要么在 a()
.
int sum();
向前声明它
另一个问题是:
if(i % 2 = 1) {
应该是:
if((i % 2) == 1) {
^~~~~ !!
哇,我发现了第三个问题:)
您尝试在 int sum()
中使用变量 number
,而该变量是在 a()
中声明的,您根本做不到。您应该通过引用将数字传递给求和(不需要 return 值,您 return 它在数字参数中):
void sum(int& number) {
并称它为:
sum(number); // this is in place of `number = sum();`
在a()
您的代码中存在许多会导致编译器错误的错误:
如果您在 int a()
中使用 sum()
函数,则需要前向声明它。
int sum();
int add() {
//
}
int sum() {
//
}
所有 sum()
然而,真正做的是将 100 添加到变量中。这可以很容易地通过 +=
运算符合并到 add()
函数中,这意味着您的代码等同于:
int a() {
int number = 0; //Added a necessary ';'
for(int i = 0; i < 20; i++) {
if(i % 2 == 1) { //Corrected this from if(i % 2 = 1)
number += 100; //No need for the sum() function
}
}
return number;
}
关键点:
您可以对您的设置执行 很多 的额外优化;本质上,int a()
可以简化为
int a() {
return 1000;
}
所以,您可以很容易地不让a()
成为一个函数:
#define a() 1000
或者,(可能更好):
const int a = 1000;
编辑:
对于更新后的代码,您需要在 int zmeratSonar1()
开始之前编写 int zmeratSonar28();
:
int zmeratSonar28();
int zmeratSonar1() {
//Sonar code stuff
}
int zmeratSonar28() {
//Other sonar code stuff
}