Declare a Const array

This is a discussion on Declare a Const array within the C++ Programming forums, part of the General Programming Boards category; Maybe this is a silly question, but I want to declare a const array: So, I do this: Code: string ...

  1. #1
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question Declare a Const array

    Maybe this is a silly question, but I want to declare a const array: So, I do this:

    Code:
    string arr[]={"A","B","C"};
    int dx[]={1,1,0,0};
    int dy[]={0,0,1,1};
    But always I received this error: a brace-enclosed initializer is not allowed here before '{' token for all 3 lines. Not matter what I change, I still cannot correct this. (Those line of code I have copied on TopCoder, and those line has successfully submit. But I don't know, when I run on my machine, It notices error)

    Who know this problem, help me please.
    thanks

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    declare const
    example:
    const int ARRAY_SIZE = 10;
    int list[ARRAY_SIZE]; //declare 10 int in an array

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    What is a "const array"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    What compiler are you using? Your example works on my compiler (g++) without any errors.

    Jim

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    I think the error is in the surrounding code. I see nothing wrong with the OP's declarations.

  6. #6
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    @laserlight: Oh, I'm sorry that I don't use correct word But I really don't know how to call this.
    My purpose (for example dx above, I want to create dx array and this array has 4 element:1,1,0,0) is obvious, but always have error and don't know to correct.

    @: I use Code::Block and mingw32 Compiler.

    Please help me, thanks

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    Show a small complete program that illustrates your problem. Also post the complete error message exactly as it appears in your development environment.

    Jim

  8. #8
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<string>
    using namespace std ;
    #define DELTA 1100
    
    class PointInPolygon {
      public:
          int A[2100][2100];
          string arr[]={"INTERIOR","BOUNDARY","EXTERIOR"};
          int dx[]={1,1,0,0};
          int dy[]={0,0,1,1};
    
            }
    this is my code.
    and this is error:
    error: a brace-enclosed initializer is not allowed here before '{' token|
    ISO C++ forbids initialization of member 'arr'|
    error: making 'arr' static|
    Last edited by hqt; 12-30-2011 at 10:05 AM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,988
    Ah, well that explains it then.
    You probably can't initialize members directly in the class yet (this is supposed to be a C++11 feature, but I dunno how support for it is yet). Have you tried getting the latest mingw compiler?
    If it still doesn't work, you will have to utilize the constructor to initialize those members.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    Yeah you can't initialize an array member in the class definition. Constant member data is supposed to be initialized in the initializer list of a constructor. Unfortunately, the syntax for the initializer list is limited to other constructor calls, which makes it impossible to initialize a constant array without also making it static. If all of the objects are going to have the same arrays then you could make it static, but it means that any objects will share one instance of the array. You could also use a container class:
    Code:
    class PointInPolygon 
    {
       private:
       const vector<int> dx;
       //...
       public:
       PointInPolygon(int _dx[], int size) : dx(_dx, _dx + size) {}
       //...
    };
    Wherever you actually write your constructor, you should pass in the array to be made constant and then call the constructor of your container class.

  11. #11
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    Oh, this code will submit to server, and maybe that server support C++0x and dont received error.
    But, with my machine, after I change my compiler (change from mingw32 to newest mingw 64bit-maybe support C++0x), I have received other error: (but same same like above)
    error: a brace-enclosed initializer is not allowed here before { token
    error: ISO C++ fordbids initialzation of member 'arr [-fpermissive]
    error: making arr static [-fpermissive]

  12. #12
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question

    Oh, this code will submit to server, and maybe that server support C++0x and dont received error.
    But, with my machine, after I change my compiler (change from mingw32 to newest mingw 64bit-maybe support C++0x), I have received other error: (but same same like above)
    error: a brace-enclosed initializer is not allowed here before { token
    error: ISO C++ fordbids initialzation of member 'arr [-fpermissive]
    error: making arr static [-fpermissive]

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    Something like this seems to work:
    Code:
    class PointInPolygon
    {
      public:
          //int A[2100][2100];
          int A[100][100];
          static const string arr[3];
          string test[3];
          const int dx[4];
          int dy[4];
          PointInPolygon() : A(), dx({1,1,0,0}), dy({0,0,1,1})
          {
             test[0] = "Interior";
             test[1] = "Boundary";
             test[2] = "Exterior";
          }
    };
    
    const string PointInPolygon::arr[3] = {"INTERIOR","BOUNDARY","EXTERIOR"};
    Watch out for those big array numbers for A, you may not have enough stack space for an array for that size. Also do you really need an array of this size in each instance of your class, or can it be shared by all instances of your class (static)?

    Jim

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    Quote Originally Posted by jimblumberg View Post
    Something like this seems to work:
    Code:
    class PointInPolygon
    {
      public:
          //int A[2100][2100];
          int A[100][100];
          static const string arr[3];
          string test[3];
          const int dx[4];
          int dy[4];
          PointInPolygon() : A(), dx({1,1,0,0}), dy({0,0,1,1})
          {
             test[0] = "Interior";
             test[1] = "Boundary";
             test[2] = "Exterior";
          }
    };
    
    const string PointInPolygon::arr[3] = {"INTERIOR","BOUNDARY","EXTERIOR"};
    Jim
    Just be aware that this is a C++0x feature that may not be what the server supports with its mingw32 installation.

    Code:
    C:\Documents and Settings\User>g++ -c -ansi a.cpp
    a.cpp: In constructor 'PointInPolygon::PointInPolygon()':
    a.cpp:13:33: warning: extended initializer lists only available with -std=c++0x
    or -std=gnu++0x
    a.cpp:13:48: warning: extended initializer lists only available with -std=c++0x
    or -std=gnu++0x
    If C++0x isn't available, then my code addresses the issue.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,988
    I am going to build upon your C++03 solution to make it better:
    Code:
    class PointInPolygon
    {
       private:
       const vector<int> dx;
       //...
       public:
       template<size_t N> PointInPolygon(int (&_dx)[N]) : dx(&_dx[0], &_dx[N]) {}
       //...
    };
    Didn't test it. Syntax errors may be embedded.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of const pointers to const struct
    By albundy in forum C Programming
    Replies: 4
    Last Post: 08-24-2011, 10:16 PM
  2. How do you declare an array in a class?
    By casablanca in forum C++ Programming
    Replies: 9
    Last Post: 08-03-2011, 01:16 PM
  3. How to declare const data member in class
    By chottachatri in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2008, 05:54 AM
  4. Declare an array in the BSS segment
    By ^xor in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 06:12 PM
  5. How to declare this array?
    By ooosawaddee3 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2002, 02:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21