Sign in to follow this  
darenking

SOLVED! Small C++ array assignment prob

Recommended Posts

darenking    122
Hi, This is a very tiny C++ problem. I have an array I want to assign several integers. I could do it like this: int animWalk[12] = { 2, 3, 4, 5, 4, 3, 2, 6, 7, 8, 7, 6 }; Trouble is, the array is actually a member of an object. I've got this in my .h file: int m_AnimWalk[12]; But when I try to fill the array with ints like this: m_AnimWalk[12] = { 2, 3, 4, 5, 4, 3, 2, 6, 7, 8, 7, 6 }; or this m_AnimWalk[] = { 2, 3, 4, 5, 4, 3, 2, 6, 7, 8, 7, 6 }; I get a syntax error. I've had to do it this way: m_AnimWalk[0] = 2; m_AnimWalk[1] = 3; m_AnimWalk[2] = 4; m_AnimWalk[3] = 5; (etc....) Is there an easier way? I definitely want an array, not a vector. These values will never change in my program. [Edited by - darenking on July 18, 2005 7:05:57 AM]

Share this post


Link to post
Share on other sites
basement    229
you could make the array static so you can initialize it (has to be somewhere outside of the class)


/* foo.hpp */
class Foo {
static int m_AnimWalk[12];
};

/* foo.cpp */
int Foo:m_animWalk[] = {1,2,3,4,5};

Share this post


Link to post
Share on other sites
xEricx    572
I think you can only initialize multiple values of an array when defining it...

As Gink said, why not initialize in your header?

Cheers

Eric

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this