简体   繁体   English

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

[英]Function/method definition error in Arduion/C++. Or syntax error?

I'm translating a program I wrote in Ruby to the Arduino/C++. 我正在将我在Ruby中编写的程序翻译成Arduino / C ++。 On my first attempt to define a function/method I keep getting the following error: "BreadboardTestFunctions:41: error: a function-definition is not allowed here before '{' token BreadboardTestFunctions:91: error: expected `}' at end of input" 在我第一次尝试定义函数/方法时,我不断收到以下错误:“BreadboardTestFunctions:41:错误:在'{'令牌BreadboardTestFunctions:91:错误:期望`}'之前,不允许使用函数定义输入”

Hard to understand because the function-definition must be followed by a pair of brackets. 难以理解,因为函数定义必须后跟一对括号。 This could reflect a syntax error as it has persisted as I've corrected a score of errors in the function as I've tried to solve this problem. 这可能反映了语法错误,因为它已经保留,因为我已经纠正了函数中的错误分数,因为我试图解决这个问题。 But it looks OK to me now. 但现在看起来对我来说还不错。

I'm generating an output after matrix multiplication in the section beginning "// routine to multiply: behavior=brain * stimulus'". 我在“//常规乘以:行为=大脑*刺激”这一部分的矩阵乘法后生成输出。 Although the problematic method, "mody" (at line 40) is only called once now, once I get it to work all the outputs will call it. 虽然有问题的方法,“mody”(第40行)现在只调用一次,一旦我开始工作,所有输出都会调用它。

The code: 编码:

    /*  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

You may not define one function inside another function. 您可能无法在另一个函数中定义一个函数。 It is what you are trying to do in the following code snippet 这是您在以下代码段中尝试执行的操作

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[])

You are trying to define function mody inside function setup. 您正在尝试在功能设置中定义功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 S函数与c ++。 建立错误 - S-Function with c++. Build error 错误:使用C ++时没有匹配的调用函数。 虽然在头文件中包含相关方法 - error: no matching function for call to, when using C++. Though include the related method in the head file 重载方法只是为了报告C ++中更好的错误。 有没有更好的办法? - Overloading a method just to report a nicer error in C++. Is there a better way? C ++中的语法错误是用}}结束类定义中的函数吗? - Is it a syntax error in C++ to end a function inside a class definition with a };? 在函数定义中指定参数默认值会导致错误C2143:语法错误:在&#39;=&#39;之前缺少&#39;)&#39; - specifying argument default value in function definition causes error C2143: syntax error : missing ')' before '=' C ++。 指向模板函数的函数指针产生未解决的重载函数类型错误 - c++. function pointer to template function produces unresolved overloaded function type error C ++中出现“ this is nullptr”错误。 链表问题 - “this was nullptr” error in C++. Linked List problems 类定义的语法错误 - Syntax error on class definition C ++。 编译错误。 正在尝试使用枚举模板参数添加朋友模板功能 - c++. compile error. am trying to add friend template function with enum template parameter C ++函数定义错误(参数传递) - C++ function definition error (parameter passing)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM