FrogInABox
FrogInABox

Reputation: 1044

Error: expected initializer before '.' token

I am fairly new to using classes and the Object Oriented side of C++ and get the error in the title.

I am writing a game of Tetris using SDL.

I have a class definition in shapes.h

class shape
{
public:
    SDL_Surface *colour;
    int rotation1[4][4];
    int rotation2[4][4];
    int rotation3[4][4];
    int rotation4[4][4];

    bool load();
    void move();

    shape();
};

and in main.h i have included shapes.h and defined instances of the class with

//Create shapes
shape O, T, S, L, R, Z, I;

I also have seperate files for each shape such as I.cpp as each shape will have different code for loading the image for its colour of block onto the SDL_Surface colour and for the various arrays of different rotations of the block, so i seperated this into one file for each block.

In I.cpp I have included main.h and tried to set up the load function for I as follows:

bool I.load()
{
    //if loading the cyan square texture fails
    if ((I.colour = surface::onLoad("../Textures/cyanSquare.png")) == NULL)
    {
        //print error
        cout << "Unable to load cyanSquare.png";
        //return fail
        return false;
    }

    I.rotation1 = {{7,7,7,7},
                   {0,0,0,0},
                   {0,0,0,0},
                   {0,0,0,0}};
    I.rotation2 = {{0,0,7,0},
                   {0,0,7,0},
                   {0,0,7,0},
                   {0,0,7,0}};
    I.rotation3 = {{7,7,7,7},
                   {0,0,0,0},
                   {0,0,0,0},
                   {0,0,0,0}};
    I.rotation4 = {{0,0,7,0},
                   {0,0,7,0},
                   {0,0,7,0},
                   {0,0,7,0}};

    return true;
}

When I try to compile this (using GCC) it reports an error on line 3 of I.cpp of:

error: expected initializer before '.' token

I have absolutely no idea what this means, and could not find anything of use searching google for this error code, so any help would be appreciated.

Upvotes: 1

Views: 15035

Answers (2)

Jay Zhu
Jay Zhu

Reputation: 1672

bool I.load()

shouldn't it be bool shape::load() ?

I is just an instance of type 'shape'. How can you use 'I' inside the function implementation as it knows nothing about the 'I' instance!

You probably want to do this: 1. Add a construct with parameter to specify the picture for the instance:

class shape
{
public:
//....

    shape(const char* pImagePath);
private:
    const char* m_pImagePath;
};

and implement the constructor as:

shape::shape(const char* image_path) : m_pImagePath(pImagePath) {}

Your load() then can be implemented as:

bool shape::load()
{
    //if loading the cyan square texture fails
    if ((colour = surface::onLoad(m_pImagePath)) == NULL)
    {
        cout << "Unable to load cyanSquare.png";
        return false;
    }

    rotation1 = {{7,7,7,7},
                 {0,0,0,0},
                 {0,0,0,0},
                 {0,0,0,0}};
    rotation2 = {{0,0,7,0},
                 {0,0,7,0},
                 {0,0,7,0},
                 {0,0,7,0}};
    rotation3 = {{7,7,7,7},
                 {0,0,0,0},
                 {0,0,0,0},
                 {0,0,0,0}};
    rotation4 = {{0,0,7,0},
                 {0,0,7,0},
                 {0,0,7,0},
                 {0,0,7,0}};

    return true;
}

Create instance 'I' as following:

shape I("../Textures/cyanSquare.png");

Upvotes: 2

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

This is not valid C++. I is a variable; you cannot define a member function for a specific variable (bool I.load()). Perhaps you meant bool shape::load()?

Upvotes: 4

Related Questions