Thread: Erm...If this isn't how you declare an array, then I'm lost

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    14

    Exclamation Erm...If this isn't how you declare an array, then I'm lost

    Code:
    #include <SDL.h>
    #include "Sprites.h"
    #include "Images.h"
    
    Sprite::Sprite(SDL_Surface *img, int dimenX, int dimenY)
    {
    	Sprite::framesF[4][2] = { { dimenX, dimenY }, { dimenX, dimenY }, { dimenX, dimenY }, { dimenX, dimenY } }; //at the first curly bracket, it's saying an expression was expected
    }
    So, I'm completely lost as to how to declare this array. Everything is already setup in the Sprites.h header, so framesF, the constructor, and class are initialized correctly. I'm just not sure why this won't take my declaration.

  2. #2
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    You can't do that because you already declared the variable framesF
    -- Will you show me how to c++?

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    14

    Angry

    Then how am I supposed to give it value? It wont let me in the header.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Assign each element individually.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by whiteflags View Post
    Assign each element individually.
    Or initialise a local array that way and then std::copy it across to the member variable.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I think the new C++ standard allows exactly that sort of initialization. you'll need to use visual studio 2010 or GCC 4.4.0 or greater (as examples... there maybe other compilers that support it) to use that new feature.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    I am using VS 2010..

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    if framesF is a member of Sprite, you're trying to assign that array (via initializer list) to index 2 of index 4 in the 2-dimensional array, which I'm guessing is not an object that can be initialized this way.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    @Elkvis, yeah, I've thought about that while I was at school..

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    try this:
    Code:
    Sprite::Sprite(SDL_Surface *img, int dimenX, int dimenY) :
        framesF({ { dimenX, dimenY }, { dimenX, dimenY }, { dimenX, dimenY }, { dimenX, dimenY } })
    {
    	
    }
    assuming that your class is declared something like this:

    Code:
    class Sprite
    {
    public:
        Sprite(SDL_Surface* img, int dimenX, int dimenY);
    private:
        int framesF[4][2];
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  3. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. structs, array, code has problems, Im lost
    By rake in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2002, 02:02 AM

Tags for this Thread