We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › unexpected token: void
Page Index Toggle Pages: 1
unexpected token: void (Read 2334 times)
unexpected token: void
Apr 24th, 2010, 8:25am
 
hey,

i'm curious as to why i always the annoying 'unexpected token: void', when i'm trying to use text in a sketch ...

- i declare my variable outside and before setup() as well as draw().
- i load my font in setup().
- i set the current font in setup().
- i use text() in draw().

isn't that the approach?

thanks in advance ...
cheers,
claus

note: below is an example that gives med the error.

Code:
PFont = font;

int columns = 20;     // number of columns
int count = 0;        // counter for color management

void setup() {
 size(800, 100);
 background(0);
 font = loadFont("arial.vlw");
 textFont(font);
 textAlign(CENTER);
}

void draw() {
 fill(0, 30);
 rect(-1, -1, width+2, height+2);
 for (int i = 0; i < width; i += width/columns) {
   fill(count*(255/columns));
   rect(i, height, width/columns, -1*(random(0.0, height)));
   text(count, i+(0.5*(width/columns)), height-20);
   count = count+1;
 }
}
Re: unexpected token: void
Reply #1 - Apr 24th, 2010, 12:04pm
 
You wrote PFont = font; instead of PFont font;
Re: unexpected token: void
Reply #2 - Apr 25th, 2010, 3:11am
 
I am also recieving "unexpected token: void" and I would appreciate it if somebody could shed some light on this. I just started processing and am working though one of the introductory books.

I presume this has something to do with the way the language itself actually works.

If I do the following, I get no problems:
Code:

int circleX = 100;
int circleY = 100;

void setup() {
size( 200, 200);
smooth();
}


but as I like to declare variable types before assigning them values ( I find it easier to read afterward) I do the following:
Code:
int circleX;
circleX = 100;
int circleY;
circleY = 100;

void setup() {
size( 200, 200);
smooth();
}


which leads to a unexpected token: void.

Could somebody shed some light on this? what am I doing wrong?

Thanks!
Re: unexpected token: void
Reply #3 - Apr 25th, 2010, 3:17am
 
You're trying to execute a statement at the global scope, as opposed to just declaring some global variables (with initial values).

Since setup() is only run once, you should do it like this:

Code:
int circleX;
int circleY;

void setup() {
 size(200, 200);
 smooth();
 circleX = 100;
 circleY = 100;
}


When Processing sees that you're trying to execute statements at the global scope, it assumes your program isn't making use of functions like setup() and draw()... which is why it has issues when you try to define setup() later.
Re: unexpected token: void
Reply #4 - Apr 25th, 2010, 3:19am
 
in the second example, you are mixing two programming modes : basic and continuous.

In continuous mode - the one with setup() - you cannot assign a value to a variable outside of a method like setup() or draw(). The "all in one declare and assign" statement is provided for convenience only.

Code:
int circleX;
int circleY;

void setup() {
size( 200, 200);
smooth();
circleX = 100;
circleY = 100;
}
Re: unexpected token: void
Reply #5 - Apr 25th, 2010, 4:55am
 
TfGuy44 & antiplastik:

Thanks for the quick replies, this clears it up. I figured that the one where a variable is declared and assigned on the same line was simply a shortcut/convenience thing, it's great to know the proper treatment.

I've bookmarked that link.

Again, thanks!
Page Index Toggle Pages: 1