Understanding the language, error messages, etc.
by jac77794 » Sat Aug 02, 2014 11:47 pm
Hi again. I was playing around and can't understand why my code won't compile when I use a struct the way it is used in other c++ tutorials or anyway I use it.
I know this code complies
- Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb = Gamebuino();
int playerx = LCDWIDTH/2 ;
int playery = (LCDHEIGHT/6)*5 ;
const byte plane[] PROGMEM = {
8,8,
B11111111,
B11111111,
B11111111,
B11111111,
B11111111,
B11111111,
B11111111,
};
void setup (){ // Setup of the game
gb.begin();
gb.titleScreen(F("TBA"));
}
void loop(){
if(gb.update()){
if(gb.buttons.repeat(BTN_RIGHT,1)){ //Make player move right
if(playerx != LCDWIDTH){ //in bound check
playerx = playerx + 1;
}
}
if(gb.buttons.repeat(BTN_LEFT,1)){ //Make player move left
if(playerx != 0){ /// in bound check
playerx = playerx -1;
}
}
}
}
But not this
- Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb = Gamebuino();
struct info {
int x;
int y;
} player;
player.x = LCDWIDTH;
player.y = LCDHEIGHT;
const byte plane[] PROGMEM = {
8,8,
B11111111,
B11111111,
B11111111,
B11111111,
B11111111,
B11111111,
B11111111,
};
void setup (){ // Setup of the game
gb.begin();
gb.titleScreen(F("TBA"));
}
void loop(){
if(gb.update()){
if(gb.buttons.repeat(BTN_RIGHT,1)){ //Make player move right
if(player.x != LCDWIDTH){ //in bound check
player.x = player.x + 1;
}
}
if(gb.buttons.repeat(BTN_LEFT,1)){ //Make player move left
if(player.x != 0){ /// in bound check
player.x = player.x -1;
}
}
}
}
sketch_aug02b:11: error: expected constructor, destructor, or type conversion before '.' token
sketch_aug02b:12: error: expected constructor, destructor, or type conversion before '.' token
sketch_aug02b.ino:15: warning: only initialized variables can be placed into program memory area
sketch_aug02b.ino: In function 'void setup()':
sketch_aug02b.ino:29: warning: only initialized variables can be placed into program memory area
Above is the error message I get. Why doesn't it work. Does the Gamebuino work differently? All help would be much appreciated.
-
jac77794
-
- Posts: 5
- Joined: Fri Aug 01, 2014 10:33 pm
by Limited » Sun Aug 03, 2014 8:39 am
Your struct and declaration of player is wrong. Try this.
- Code: Select all
typedef struct {
int x;
int y;
} Player;
Player player;
player.x = LCDWIDTH;
player.y = LCDHEIGHT;
-
Limited
-
- Posts: 13
- Joined: Sat May 31, 2014 7:25 pm
by jac77794 » Sun Aug 03, 2014 11:53 am
Thanks for the advice. I tried it and it still won't compile.
- Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb = Gamebuino();
typedef struct {
int x;
int y;
} Player;
Player player;
player.x = LCDWIDTH;
player.y = LCDHEIGHT;
const byte plane[] PROGMEM = {
8,8,
B11111111,
B11111111,
B11111111,
B11111111,
B11111111,
B11111111,
B11111111,
};
void setup (){ // Setup of the game
gb.begin();
gb.titleScreen(F("TBA"));
}
void loop(){
if(gb.update()){
if(gb.buttons.repeat(BTN_RIGHT,1)){ //Make player move right
if(player.x != LCDWIDTH){ //in bound check
player.x = player.x + 1;
}
}
if(gb.buttons.repeat(BTN_LEFT,1)){ //Make player move left
if(player.x != 0){ /// in bound check
player.x = player.x -1;
}
}
}
}
These are the error messages that I am getting.
sketch_aug02b:14: error: expected constructor, destructor, or type conversion before '.' token
sketch_aug02b:15: error: expected constructor, destructor, or type conversion before '.' token
sketch_aug02b.ino:18: warning: only initialized variables can be placed into program memory area
sketch_aug02b.ino: In function 'void setup()':
sketch_aug02b.ino:32: warning: only initialized variables can be placed into program memory area
-
jac77794
-
- Posts: 5
- Joined: Fri Aug 01, 2014 10:33 pm
by rodot » Sun Aug 03, 2014 12:00 pm
That's because you can't run instruction out of any function, you have to place them either in setup() or loop() (or another function).
So if you put the following in setup() instead of global scope it compiles fine
- Code: Select all
player.x = LCDWIDTH;
player.y = LCDHEIGHT;
Here is the corrected code:
- Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb = Gamebuino();
typedef struct {
int x;
int y;
} Player;
Player player;
const byte plane[] PROGMEM = {
8,8,
B11111111,
B11111111,
B11111111,
B11111111,
B11111111,
B11111111,
B11111111,
};
void setup (){ // Setup of the game
gb.begin();
gb.titleScreen(F("TBA"));
player.x = LCDWIDTH;
player.y = LCDHEIGHT;
}
void loop(){
if(gb.update()){
if(gb.buttons.repeat(BTN_RIGHT,1)){ //Make player move right
if(player.x != LCDWIDTH){ //in bound check
player.x = player.x + 1;
}
}
if(gb.buttons.repeat(BTN_LEFT,1)){ //Make player move left
if(player.x != 0){ /// in bound check
player.x = player.x -1;
}
}
}
}
-
rodot
- Site Admin
-
- Posts: 1290
- Joined: Mon Nov 19, 2012 11:54 pm
- Location: France
-
by jac77794 » Sun Aug 03, 2014 12:06 pm
Thanks rodot it works great now.
Also I am loving my Gamebuino thanks for starting this project it is giving an accessible and engaging platform to learn to code.
-
jac77794
-
- Posts: 5
- Joined: Fri Aug 01, 2014 10:33 pm
Return to Programming Questions
Who is online
Users browsing this forum: No registered users and 70 guests