c++ - Error: expected initializer before '.' token -


i new using classes , object oriented side of c++ , error in title.

i writing game of tetris using sdl.

i have 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 have included shapes.h , defined instances of class with

//create shapes shape o, t, s, l, r, z, i; 

i have seperate files each shape such i.cpp each shape have different code loading image colour of block onto sdl_surface colour , various arrays of different rotations of block, seperated 1 file each block.

in i.cpp have included main.h , tried set load function follows:

bool i.load() {     //if loading cyan square texture fails     if ((i.colour = surface::onload("../textures/cyansquare.png")) == null)     {         //print error         cout << "unable 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 try compile (using gcc) reports error on line 3 of i.cpp of:

error: expected initializer before '.' token 

i have absolutely no idea means, , not find of use searching google error code, appreciated.

bool i.load() 

shouldn't bool shape::load() ?

i instance of type 'shape'. how can use 'i' inside function implementation knows nothing 'i' instance!

you want this: 1. add construct parameter specify picture instance:

class shape { public: //....      shape(const char* pimagepath); private:     const char* m_pimagepath; }; 

and implement constructor as:

shape::shape(const char* image_path) : m_pimagepath(pimagepath) {} 

your load() can implemented as:

bool shape::load() {     //if loading cyan square texture fails     if ((colour = surface::onload(m_pimagepath)) == null)     {         cout << "unable 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' following:

shape i("../textures/cyansquare.png"); 

Comments