Hello, I use Mac OS Catalina, and I’m a Japanese beginner, trying to figure out how to use OpenFrameworks using a textbook published 4 years ago.
I am sorry if my English may not be good enough to explain what my problem is, but I hope someone can help me out…!
I am trying to make an animation with multiple circles, but Xcode says “function definition is not allowed here” in
section such as
App::update()
and other void of section, like ofApp::draw(), ofApp::keyPressed( int key) and so on.
This started to happen after using the formula
static const int NUM = 100;
but I’m not sure if this is the cause , or if the textbook I’m using is too old… Here is my code.
#include "ofApp.h"
static const int NUM = 100;
float loc_x[NUM];
float loc_y[NUM];
float speed_x[NUM];
float speed_y[NUM];
//--------------------------------------------------------------
void ofApp::setup()
{
ofBackground(0, 0, 0);
ofSetFrameRate(30);
ofSetCircleResolution(64);
ofEnableAlphaBlending();
int i;
for(i=0; i<100; i++){
loc_x[i]=ofRandom(0, ofGetWidth());
loc_y[i]=ofRandom(0, ofGetHeight());
speed_x[i]=ofRandom(-10,10)+5;
speed_y[i]=ofRandom(-10,10)+10;
}
//--------------------------------------------------------------
void ofApp::update(){
for (i=0; i<NUM; i++){
loc_x[i]=loc_x[i]+speed_x[i];
loc_y[i]=loc_y[i]+speed_y[i];
if (loc_x[i]<0){speed_x[i]=speed_x[i]*-1;}
if (loc_x[i]>ofGetWidth()){speed_x[i]=speed_x[i]*-1;}
if(loc_y[i]<0){speed_y[i]=speed_y[i]*-1;}
if (loc_y[i]>ofGetHeight()){speed_y[i]=speed_y[i]*-1;}
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(31, 63, 255, 127);
for ( int i=0; i<NUM; i++){
ofDrawCircle(loc_x[i], loc_y[i], 40);}
}
//--------------------------------------------------------------

Hi,
you are missing a }
at the end of ofApp::setup()
.
You get this error when you try to define a function inside another function, which often happens because you’re missing a closing curly bracket.
This is how it should be:
#include "ofApp.h"
static const int NUM = 100;
float loc_x[NUM];
float loc_y[NUM];
float speed_x[NUM];
float speed_y[NUM];
//--------------------------------------------------------------
void ofApp::setup()
{
ofBackground(0, 0, 0);
ofSetFrameRate(30);
ofSetCircleResolution(64);
ofEnableAlphaBlending();
int i;
for(i=0; i<100; i++){
loc_x[i]=ofRandom(0, ofGetWidth());
loc_y[i]=ofRandom(0, ofGetHeight());
speed_x[i]=ofRandom(-10,10)+5;
speed_y[i]=ofRandom(-10,10)+10;
}
}
//--------------------------------------------------------------
void ofApp::update(){
for (int i=0; i<NUM; i++){
loc_x[i]=loc_x[i]+speed_x[i];
loc_y[i]=loc_y[i]+speed_y[i];
if (loc_x[i]<0){speed_x[i]=speed_x[i]*-1;}
if (loc_x[i]>ofGetWidth()){speed_x[i]=speed_x[i]*-1;}
if(loc_y[i]<0){speed_y[i]=speed_y[i]*-1;}
if (loc_y[i]>ofGetHeight()){speed_y[i]=speed_y[i]*-1;}
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(31, 63, 255, 127);
for ( int i=0; i<NUM; i++){
ofDrawCircle(loc_x[i], loc_y[i], 40);
}
}
Thank you so much, it mostly worked!!
I did miss to put a }
… it cleared almost all the errors, except
<function definition is not allowed here>
is still here remaining at the
void ofApp::windowResized( int w, int h),
ofApp::gotMessage(ofMessage msg),
ofApp::dragEvent(ofDragInfo dragInfo)
and also, expected '}'
at the end.
void ofApp::windowResized( **int** w, **int** h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
I feel sad that I have to bug you guys for such an elementary questions…
thank you for helping!!
I deleted the space between the {}
and the problem went away… Thank you for helping.