Picosystem Compilation Error – does not name type

Hello everyone,

I’m new to the Picosystem, C++ and OOP – so what could go wrong? I want to create 2 alien sprites and display them on the screen using a class and objects. I’ve copied an example from this website C++ Classes and Objects and modified it but when I try to compile I get the errors below. I’ve looked at some other examples and noticed they put a forward declaration in but that hasn’t worked either.

Thanks for any help,

Leaf-frog

Compilation error ----------->

/home/leaffrog/ourproject/main.cpp:19:1: error: ‘space_alien1’ does not name a type
19 | space_alien1.x = 8;
| ^~~~~~~~~~~~
/home/leaffrog/ourproject/main.cpp:20:1: error: ‘space_alien1’ does not name a type
20 | space_alien1.y = 8;
| ^~~~~~~~~~~~
/home/leaffrog/ourproject/main.cpp:22:1: error: ‘space_alien2’ does not name a type
22 | space_alien2.x = 8+9;
| ^~~~~~~~~~~~
/home/leaffrog/ourproject/main.cpp:23:1: error: ‘space_alien2’ does not name a type
23 | space_alien2.y = 8;
| ^~~~~~~~~~~~

The Code ---------------->

// Forward declaration
class Alien;

#include “picosystem.hpp”

using namespace picosystem;

#define alien_sprite 89

class Alien {
public:
int x;
int y;
};

Alien space_alien1;
Alien space_alien2;

space_alien1.x = 8;
space_alien1.y = 8;

space_alien2.x = 8+9;
space_alien2.y = 8;

// ****************
// * Initialise *
// ****************
void init() {
}

// ****************
// * Update *
// ****************
void update(uint32_t tick) {
}

// ****************
// * Draw *
// ****************
void draw(uint32_t tick) {
pen(0, 0, 0);
clear();

pen(15, 15, 15);
text(“Hello, world!”, 0, 0);
sprite(alien_sprite, space_alien1.x, space_alien1.y);
sprite(alien_sprite, space_alien2.x, space_alien2.y);
}

FYI, if you surround your code with sets of 3 backticks (```) it will format as code, and is a bit more readable:

int main{
    printf("Hello world!");

    return 0;
}

The problem seems to be that your declaration of space_alien1 and space_alien2 aren’t part of a function or class anywhere. Try doing this in the same way that the W3 example has their declarations wrapped in int main(){code here}.

I think C++ only allows code such as declarations outside a function, the actuall running code needs to be inside one.

Thanks for the help. I’ll look for main and copy the W3 example more closely. I’ll also try the 3 backticks out if I paste code.

What he said, regarding code tags. It’s the key top left under the Esc key with a ~ on it. On an English US keyboard anyway. Really wish there was a “code button”. Hint hint !!