Initializing 2D Static Array from Constructor

This may seem like an odd question, but I simply cannot get my 2D array to initialize. In fact, I can't even use it. I declare it and set its size in the class declaration of class Block, then initialize each value in the constructor thereof. I am using it to store the vertex locations of eight vertices for a cube. Here's the code where I declare the array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Class Block
{
     public:
          Block();
     protected:
          static int verticies[8][3];
};

//constructor implementation:

Block::Block()
{
     verticies[1][2] = 1;
     verticies[2][1] = 1;
     verticies[3][0] = 1;
     verticies[4][1] = 1;
     verticies[4][2] = 1;
     verticies[5][0] = 1;
     verticies[5][1] = 0;
     verticies[6][0] = 1;
     verticies[6][2] = 1;
     verticies[7][0] = 1;
     verticies[7][1] = 1;
     verticies[7][2] = 1;
}


I get two errors:
[Linker Error] undefined reference to 'vtable for Block'
[Linker Error] undefined reference to 'Block::verticies'

(actually I get a lot of the second errors, but they're all the same)

I'm only initializing a few of the places in verticies because they're the vertex locations for a cube with an area of 1. I'm going to use this data to render a cube using the OpenGL library.

There's also another problem I'm having. In a class that inherits from Block, Dirt, I am unable to use the verticies array. I get the exact same error, in fact. The exact code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Dirt::display()
{
     glPushMatrix();
     //glMatrixMode( TRANSFORMATION_MATRIX );
     glLoadIdentity();
     glTranslatef( x, y, z );
     glColor3f( red, green, blue );
     glBegin( GL_POLYGON );
          for( int i = 0; i < 6; i++ )
          {
               if( i == 0 )
                   glColor3f( 0.0f, 1.0f, 0.0f );
               else
                   glColor3f( red, green, blue );
               glVertex3iv( &verticies[faceIndicies[i][0]][0] );
               glVertex3iv( &verticies[faceIndicies[i][1]][0] );
               glVertex3iv( &verticies[faceIndicies[i][2]][0] );
               glVertex3iv( &verticies[faceIndicies[i][3]][0] );
          }
     glEnd();
     glPopMatrix();
}


I am using DevC++ 4.9.9.2 to create this program in, if that helps at all.

Why will this not work?
I think that the static array cannot be initialize in the way you have in the constuctor . (Correct me it i am wrong ) . Is should be initialized outside the class first , just like the static variable .

1
2
3
4
5
6
7
class Foo
{
  private:
    static const int twoD[2][2];
  public:
    int getTwoD(const int & i, const int & j);
};



1
2
3
4
5
6
7
8
9
10
11
12
const int Foo::twoD[2][2] = { {1,2},{3,4} };



int Foo::getTwoD(const int & i, const int & j)
{
    if ((i < 0) || (i >= 2) || (j < 0) || (j >= 2)) {
        cerr << "Illegal index: valid values are 0 and 1" << endl;
        exit(EXIT_FAILURE);
    }
    return twoD[i][j];
}

Last edited on
Thank you! Now my array works! I have a few other problems, though, but I'll put them in another topic.
Topic archived. No new replies allowed.