Thread: syntax for initializing an array in a class

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    12

    Question syntax for initializing an array in a class

    I have been studying the DirectX tutorials trying to learn graphics programming and struck out on my own trying to create a tetris clone to learn from. I would like to build the shapes using classes unlike the M$ way of using everything global in the tutorials. What is the proper way of initializing an array of structs in a class? The compiler keeps giving me a syntax error of missing a semi-colon. Right now I have a decent workable engine for my game, but it just doesn't feel right using all these globals.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post what you've tried so we can help you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    My first time posting code.....here's a try..

    Code:
    struct CUSTOMVERTEX
    {
        float x, y, z, rhw; // The transformed position for the vertex
        DWORD color;        // The vertex color
    
    };
    
    class shape1
    {
    
    private:
    
    CUSTOMVERTEX line[] = //currently a line strip
    {
    	{ 72.0, 0.0, 0.5, 1.0, 0xffff0000,  },
    	{ 72.0, 24.0, 0.5, 1.0, 0xffff0000,  },
    
    	{ 168.0, 24.0, 0.5, 1.0, 0xffff0000,  },
    	{ 168.0, 0.0, 0.5, 1.0, 0xffff0000,  },
    
    	{ 72.0, 0.0, 0.5, 1.0, 0xffff0000, },
    
    };
    
    public:
    	void memberfunctiongoeshere()
    	{
    
    	}
    
    }; //endclass
    Compiler error:
    Code:
    C:\Documents and Settings\Administrator\Desktop\adian\adian.cpp(70) : error C2059: syntax error : '{'
    Note: I have tried taking the ; out, putting it back in, taking the comas out.....etc...etc....etc...

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You can't initailise at that point in the code. You need to do it in a constructor.

    Here's an example showing two ways:
    Code:
    #include <iostream>
    
    struct myStruct
    {
      int i;
      myStruct() : i(0) {}; 
    };
    
    class myClass
    {
      private:
      myStruct myStructArray[10];
      public:
      
      myClass() {};
      
      explicit myClass(int num)
      {
        for (int i = 0; i < 10; ++i)
        {
          myStructArray[i].i = num;
        }
      }
      
      void printArray()
      {
        for (int i = 0; i < 10; i++)
          std::cout <<"element " <<i <<" is " <<myStructArray[i].i <<std::endl;
      }
      
    };
    
    int main()
    {
      myClass c1, c2(2);
      c1.printArray();
      c2.printArray();
    }
    
    /*
    element 0 is 0
    element 1 is 0
    element 2 is 0
    element 3 is 0
    element 4 is 0
    element 5 is 0
    element 6 is 0
    element 7 is 0
    element 8 is 0
    element 9 is 0
    element 0 is 2
    element 1 is 2
    element 2 is 2
    element 3 is 2
    element 4 is 2
    element 5 is 2
    element 6 is 2
    element 7 is 2
    element 8 is 2
    element 9 is 2
    */
    In the example:
    Method 1:
    Using the default myClass constructor calls the struct constructor to initialise all elements to zero.

    Method 2:
    Using the int constructor for myClass performs a loop around the structs and applies num to each element. If you use this method, the constructor in the struct itself isn't necessary.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    Thanks, I didn't know I couldn't initialize it like that in a class, which means I'll have to do it the hard way. Thank you for your time and knowledge.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM