Unable to initialize bool array

Trying to simply set all values in the array to false without a loop

Page 1 of 1

11 Replies - 11151 Views - Last Post: 17 November 2010 - 11:01 AM Rate Topic: -----

#1 reaper4334  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 107
  • Joined: 01-November 09

Unable to initialize bool array

Posted 17 November 2010 - 09:55 AM

As far as I can tell, this should work:

bool keys[256] = { false };



and I get the following errors
error C2059: syntax error : '{'	\application.h	22
error C2334: unexpected token(s) preceding '{'; skipping apparent function body	\application.h	22



I've also tried
bool keys[256] = { };
bool keys[256] = { 0 };



whole source
#ifndef _APPLICATION_
#define _APPLICATION_

#include "GLwindow.h"

class Application
{
public:
	Application();
	~Application();

	void Init();
	void Update(float deltaTime);
	void Render();

	void mouseDown();
	void mouseUp();
	void keyDown(int index);
	void keyUp(int index);
private:
	GLWindow window;
	bool keys[256] = { 0 }; // an array of whether or not keys are pressed
	float angle;
};

#endif



Is This A Good Question/Topic? 0
  • +

Replies To: Unable to initialize bool array

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Unable to initialize bool array

Posted 17 November 2010 - 10:04 AM

you cant initialize it the class definition. you have to initialize it the constructor. unfortunately you cant use a list internalizer to initialize an array after it has been declared. this is where loops come in.

edit: not sure if this syntax will work but it might be worth a try

class foo {
public:
   bool array[256];
   foo() : array({false}) {
      //some other code to initialize with
   }
};

This post has been edited by ishkabible: 17 November 2010 - 10:10 AM

Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Unable to initialize bool array

Posted 17 November 2010 - 10:05 AM

You cannot initialize the variable inside the class definition. Use initialization list on your constructor or initialize it in the constructor.


Jim
Was This Post Helpful? 0
  • +
  • -

#4 reaper4334  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 107
  • Joined: 01-November 09

Re: Unable to initialize bool array

Posted 17 November 2010 - 10:10 AM

I suppose I'll just have to go with a loop then :(

Thanks :)
Was This Post Helpful? 0
  • +
  • -

#5 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Unable to initialize bool array

Posted 17 November 2010 - 10:16 AM

wait a sec, i found a few places where they used this syntax above by casting it it the appropriate type also i found on place that showed it like this

class foo {
public:
   bool array[256];
   foo() : array(){false} {
      //some other code to initialize with
   }
};




note: i am not saying that it will work, just something to try.

This post has been edited by ishkabible: 17 November 2010 - 10:18 AM

Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,597
  • Joined: 08-August 08

Re: Unable to initialize bool array

Posted 17 November 2010 - 10:26 AM

I've noticed that even outside of a class, initializing an array doesn't work as expected:
#include <iostream>

using namespace std;

int main()
{
	bool keys[256]= {true}; // same results with {1}
	cout << keys[1] << endl;
	memset(keys, 1, sizeof(keys));
	cout << keys[1] << endl;
	return 0;
}


My output
0
1
:wacko:
Was This Post Helpful? 0
  • +
  • -

#7 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 232
  • Joined: 07-July 10

Re: Unable to initialize bool array

Posted 17 November 2010 - 10:28 AM

View Postishkabible, on 17 November 2010 - 09:04 AM, said:

you cant initialize it the class definition. you have to initialize it the constructor. unfortunately you cant use a list internalizer to initialize an array after it has been declared. this is where loops come in.

edit: not sure if this syntax will work but it might be worth a try

class foo {
public:
   bool array[256];
   foo() : array({false}) {
      //some other code to initialize with
   }
};


I tested this with TDM-GCC 4.5.1 and can confirm it works. It appears to be a c++0x feature.
Was This Post Helpful? 0
  • +
  • -

#8 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Unable to initialize bool array

Posted 17 November 2010 - 10:36 AM

ive read that c++0x improves a lot of things with array lists. one of the things begin that you can pass an array list as an array or structure for an argument. i think that is a nice feature because it avoids having to use a constructor and declaring an array just to pass it to a function.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,549
  • Joined: 23-August 08

Re: Unable to initialize bool array

Posted 17 November 2010 - 10:42 AM

That behavior, CTphpnwb, is as defined in the standard:

Quote

If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly
initialized shall be value-initialized (8.5). [ Example: struct S { int a; char* b; int c; };
S ss = { 1, "asdf" };
initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an expression of the form int(), that is, 0. — end example ]


So apparently the default value-initializer for bool is false.

#include <iostream>

using namespace std;

int main()
{
    bool test1[16] = { false };
    bool test2[16] = { true };
    bool test3[16];

    cout << "Test1 - Init to false" << endl;
    for (size_t i = 0; i < sizeof(test1)/sizeof(test1[0]); ++i)
        cout << test1[i];

    cout << endl << "Test2 - Init to true" << endl;
    for (size_t i = 0; i < sizeof(test2)/sizeof(test2[0]); ++i)
        cout << test2[i];

    cout << endl << "Test3 - Uninitialized" << endl;
    for (size_t i = 0; i < sizeof(test3)/sizeof(test3[0]); ++i)
        cout << test3[i];

    cout << endl;
}



Result:

./arrtest
Test1 - Init to false
0000000000000000
Test2 - Init to true
1000000000000000
Test3 - Uninitialized
12024619195255127009671929525512700

Was This Post Helpful? 3
  • +
  • -

#10 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Unable to initialize bool array

Posted 17 November 2010 - 10:51 AM

since this is the case a loop would be best because it would be a royal pain to explicitly fill in each element of any array. it would also be very hard to read obliviously.
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,597
  • Joined: 08-August 08

Re: Unable to initialize bool array

Posted 17 November 2010 - 10:55 AM

What's wrong with memset?
Was This Post Helpful? 1
  • +
  • -

#12 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Unable to initialize bool array

Posted 17 November 2010 - 11:01 AM

ya your write much better than writing a loop every time. i don't use the c libray that much so didn't think about that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1