gpt4 book ai didi

c++ - Arduino/C++ 中的函数/方法定义错误。还是语法错误?

转载 作者:行者123 更新时间:2023-11-28 03:02:58 25 4
gpt4 key购买 nike

我正在将我用 Ruby 编写的程序翻译成 Arduino/C++。在我第一次尝试定义函数/方法时,我不断收到以下错误:“BreadboardTestFunctions:41: error: a function-definition is not allowed here before '{' tokenBreadboardTestFunctions:91:错误:输入末尾应为“}””

很难理解,因为函数定义后面必须跟一对括号。这可能反射(reflect)了一个语法错误,因为它一直存在,因为我在尝试解决这个问题时纠正了函数中的一些错误。但现在我觉得还可以。

我在以“//routine to multiply: behavior=brain * stimulus'”开头的部分中生成矩阵乘法后的输出。虽然有问题的方法“mody”(在第 40 行)现在只被调用一次,但一旦我让它工作,所有输出都会调用它。

代码:

    /*  BREADBOARD
Implement program on Arduino + breadboard
*/

// constants
int foodPin = 2; // to provide food
int painPin = 3; // to punish
int ucsPin = 4; // the UCS
int csPin = 5; // the CS
int lightPin = 6; // turn the "light" stim on/off
int thresh = 700;

// variables
int buttonState = 0; // variable for reading the pushbutton status
boolean lighton = false;
unsigned short int energy = 10000;
int stimulus[11] = {0,0,0,0,0,0,0,0,0,0,0};

int brain[7][11] = { {0,0,0,0,99,0,0,0,0,1,0},
{0,0,0,0,0,99,0,0,0,1,0},
{0,0,0,0,0,0,99,0,0,1,0},
{90,0,0,0,0,0,0,1,-1,1,-99},
{0,90,0,0,0,0,0,1,-1,1,1},
{0,0,90,0,0,0,0,1,-1,1,1},
{0,0,0,90,0,0,0,1,-1,1,1} };

int behavior[7] = {0,0,0,0,0,0,0};

void setup() {
// initialize the pushbutton pins as an input:
pinMode(foodPin, INPUT);
pinMode(painPin, INPUT);
pinMode(ucsPin, INPUT);
pinMode(csPin, INPUT);
pinMode(lightPin, INPUT);
Serial.begin(9600);
int ix=0;

// define behavioral methods
void mody (int ix, int brain[], int stimulus[])
{ int psp=20;
int j;
for(j=7;j<11;j++)
{if (brain[ix][j] > 0) brain[ix][j]+= stimulus[j] * (99-brain[ix][j])/psp;
if (brain[ix][j] < 0) brain[ix][j]+= -1*(stimulus[j] * abs(99-brain[ix][j])/psp);}
return;}

} // end void setup

void loop(){
// decay stimulus vector. do this and check inputs for ALL stimulii later
int k;
for(k=0;k<11;k++)
{if (stimulus[k] > 1) stimulus[k]-=2; else stimulus[k]=0;}

//check inputs

buttonState = digitalRead(foodPin);
if (buttonState == HIGH) stimulus[4] = 9;
buttonState = digitalRead(painPin);
if (buttonState == HIGH) stimulus[5] = 9;
buttonState = digitalRead(ucsPin);
if (buttonState == HIGH) stimulus[6] = 9;
buttonState = digitalRead(lightPin);
if (buttonState == HIGH) {stimulus[7] = 9; stimulus[8] = 9;lighton = true;}
else {stimulus[7] = 0; stimulus[8] = 0;lighton = false;}
buttonState = digitalRead(ucsPin);
if (buttonState == HIGH) stimulus[6] = 9;

// routine to multiply: behavior=brain * stimulus'
int i, j;
for(i=0;i<7;i++)
{ behavior[i]=0;
for (j=0;j<11;j++)
{behavior[i]= behavior[i]+stimulus[j]*brain[i][j]; }
} // end for i
if (behavior[0] > thresh) {Serial.println("Positive Fixer");}
if (behavior[1] > thresh) {Serial.println("Negative Fixer");}
if (behavior[2] > thresh) {Serial.println("UCR"); mody (2, brain[], stimulus[]);}
if (behavior[3] > thresh) {Serial.println("Operant one");}
if (behavior[4] > thresh) {Serial.println("Operant two");}
if (behavior[5] > thresh) {Serial.println("Operant three");}
if (behavior[6] > thresh) {Serial.println("Operant four");}

// generate random operant
if (random(energy) < 10) stimulus[random(4)]= 9 + random(3);

energy --;
Serial.println(energy);

} // end void loop

最佳答案

您不能在另一个函数中定义一个函数。这是您在以下代码片段中尝试做的事情

void setup() {
// initialize the pushbutton pins as an input:
pinMode(foodPin, INPUT);
pinMode(painPin, INPUT);
pinMode(ucsPin, INPUT);
pinMode(csPin, INPUT);
pinMode(lightPin, INPUT);
Serial.begin(9600);
int ix=0;

// define behavioral methods
void mody (int ix, int brain[], int stimulus[])

您正在尝试在函数设置中定义函数 mody。

关于c++ - Arduino/C++ 中的函数/方法定义错误。还是语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20313178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com