Using constant variables in header file

Discussion in 'C++' started by Karen, Nov 12, 2003.

  1. Karen

    Karen Guest

    Hi,

    I have one constant variable and want to use it in two files. I put it in
    the header file and then include the header file. The compiler always say
    "error C2370: 'arraysize' : redefinition; different storage class". What
    shall I do? Files are listed below. I'm using Visual C++ 6.0

    By the way, how to use STL (vector , for example) to create a
    multi-dimension array?

    Many thanks.

    Karen
    ---------------------------------
    my header file - consts.h:
    const int arraysize = 15;

    my sources file 1 - op1.cpp:

    #include consts.h
    char array1[arraysize];

    my source file 2 - op2.cpp:

    #include consts.h
    char array2[arraysize];
    Karen, Nov 12, 2003
    #1
    1. Advertising

  2. "Karen" <> wrote...
    > I have one constant variable and want to use it in two files. I put it in
    > the header file and then include the header file. The compiler always say
    > "error C2370: 'arraysize' : redefinition; different storage class". What
    > shall I do? Files are listed below. I'm using Visual C++ 6.0
    >
    > By the way, how to use STL (vector , for example) to create a
    > multi-dimension array?
    >
    > Many thanks.
    >
    > Karen
    > ---------------------------------
    > my header file - consts.h:
    > const int arraysize = 15;
    >
    > my sources file 1 - op1.cpp:
    >
    > #include consts.h
    > char array1[arraysize];
    >
    > my source file 2 - op2.cpp:
    >
    > #include consts.h
    > char array2[arraysize];


    The only error I can see in those two translation units is that
    the #include directory doesn't have the header name in either
    double quotes or angle brackets. Beyond that, everything should
    be fine.

    Perhaps you should post the actual code that produces the error.

    To create a multidimensional array from std::vector, create
    a vector of vectors [of vectors ...] of the needed type:

    std::vector<std::vector< ... <int> ... > > mda;

    Don't forget to resize the mda. Or you could initialise it
    with needed sizes for each dimension.

    Victor
    Victor Bazarov, Nov 12, 2003
    #2
    1. Advertising

  3. "Karen" <> wrote...
    > I have one constant variable and want to use it in two files. I put it in
    > the header file and then include the header file. The compiler always say
    > "error C2370: 'arraysize' : redefinition; different storage class". What
    > shall I do? Files are listed below. I'm using Visual C++ 6.0
    >
    > By the way, how to use STL (vector , for example) to create a
    > multi-dimension array?
    >
    > Many thanks.
    >
    > Karen
    > ---------------------------------
    > my header file - consts.h:
    > const int arraysize = 15;
    >
    > my sources file 1 - op1.cpp:
    >
    > #include consts.h
    > char array1[arraysize];
    >
    > my source file 2 - op2.cpp:
    >
    > #include consts.h
    > char array2[arraysize];


    The only error I can see in those two translation units is that
    the #include directory doesn't have the header name in either
    double quotes or angle brackets. Beyond that, everything should
    be fine.

    Perhaps you should post the actual code that produces the error.

    To create a multidimensional array from std::vector, create
    a vector of vectors [of vectors ...] of the needed type:

    std::vector<std::vector< ... <int> ... > > mda;

    Don't forget to resize the mda. Or you could initialise it
    with needed sizes for each dimension.

    Victor
    Victor Bazarov, Nov 12, 2003
    #3
  4. Karen <> writes
    >I have one constant variable and want to use it in two files. I put it in
    >the header file and then include the header file. The compiler always say
    >"error C2370: 'arraysize' : redefinition; different storage class". What
    >shall I do? Files are listed below. I'm using Visual C++ 6.0


    >---------------------------------
    >my header file - consts.h:
    >const int arraysize = 15;


    should that be:
    static const int arraysize = 15;

    By omitting the 'static' keyword, you're defining a variable with
    external linkage. You're only allowed to do this once.

    --
    Simon Elliott
    http://www.ctsn.co.uk/
    Simon Elliott, Nov 12, 2003
    #4
  5. "Simon Elliott" <> wrote...
    > Karen <> writes
    > >I have one constant variable and want to use it in two files. I put it in
    > >the header file and then include the header file. The compiler always say
    > >"error C2370: 'arraysize' : redefinition; different storage class". What
    > >shall I do? Files are listed below. I'm using Visual C++ 6.0

    >
    > >---------------------------------
    > >my header file - consts.h:
    > >const int arraysize = 15;

    >
    > should that be:
    > static const int arraysize = 15;


    Doesn't have to.

    >
    > By omitting the 'static' keyword, you're defining a variable with
    > external linkage. You're only allowed to do this once.


    See 3.5/3. 'const' objects have internal linkage if _not_ declared
    'extern' and _not_ previously declared to have external linkage.

    Victor
    Victor Bazarov, Nov 12, 2003
    #5
  6. Karen

    Adam Fineman Guest

    Karen wrote:
    > Hi,
    >
    > I have one constant variable and want to use it in two files. I put it in
    > the header file and then include the header file. The compiler always say
    > "error C2370: 'arraysize' : redefinition; different storage class". What
    > shall I do? Files are listed below. I'm using Visual C++ 6.0
    >
    > By the way, how to use STL (vector , for example) to create a
    > multi-dimension array?
    >
    > Many thanks.
    >
    > Karen
    > ---------------------------------
    > my header file - consts.h:
    > const int arraysize = 15;
    >
    > my sources file 1 - op1.cpp:
    >
    > #include consts.h


    #include "consts.h" // you forgot the quotes

    > char array1[arraysize];
    >
    > my source file 2 - op2.cpp:
    >
    > #include consts.h

    #include "consts.h"

    > char array2[arraysize];
    >


    The reason for the multiple definition error is that you need what's
    referred to as an include guard:

    //////////////////////////
    // consts.h

    #ifndef CONSTS_H_INCLUDED
    #define CONSTS_H_INCLUDED

    const int arraysize = 15;

    #endif // include guard
    /////////////////////////

    What that does is ensures that the header file is only included once in
    each compilation unit. Try that in your program, and then read up on
    include guards in your favorite reference book. There may also be
    something in the newsgroup's FAQ.

    - Adam

    --
    Reverse domain name to reply.
    Adam Fineman, Nov 12, 2003
    #6
  7. Karen

    Adam Fineman Guest

    STL multi-dimensional arrays (Was: Using constant variables in headerfile)

    Karen wrote:
    <snip>
    >
    > By the way, how to use STL (vector , for example) to create a
    > multi-dimension array?
    >

    <snip>

    Something like this:

    //////////////////////////////////////////////////////////////
    #include <vector>
    #include <iostream>
    #include <iterator>

    int
    main()
    {
    typedef std::vector< std::vector<int> > table_t;

    table_t table;

    for (int i = 1; i <= 9; ++i)
    {
    std::vector<int> row;

    for (int j = 1; j <= 9; ++j)
    {
    row.push_back(i * j);
    }

    table.push_back(row);
    }

    int factor1 = 6, factor2 = 8;
    std::cout << factor1 << " times " << factor2 << " is "
    << table[factor1 - 1][factor2 - 1]
    << "\n\n";

    for (table_t::const_iterator r = table.begin();
    r != table.end();
    ++r)
    {
    copy(r->begin(), r->end(),
    std::eek:stream_iterator<int>(std::cout, "\t"));
    std::cout << '\n';
    }

    return 0;
    }
    //////////////////////////////////////////////////////////////

    - Adam
    --
    Reverse domain name to reply.
    Adam Fineman, Nov 12, 2003
    #7
  8. Karen

    Jon Bell Guest

    Re: STL multi-dimensional arrays (Was: Using constant variables in headerfile)

    In article <bxusb.622$>,
    Adam Fineman <> wrote:
    [snip]
    > typedef std::vector< std::vector<int> > table_t;
    >
    > table_t table;
    >
    > for (int i = 1; i <= 9; ++i)
    > {
    > std::vector<int> row;
    >
    > for (int j = 1; j <= 9; ++j)
    > {
    > row.push_back(i * j);
    > }
    >
    > table.push_back(row);
    > }


    Or if you know the number of rows and columns in advance, you can
    construct the vector of vectors with that size:

    vector<vector<int> > table (numRows, vector<int>(numCols))

    then fill it up just like you would an array.

    for (int row = 0; row < numRows; ++row)
    {
    for (col = 0; col < numCols; ++col)
    {
    table[row][col] = row * col;
    }
    }

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Nov 13, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    4
    Views:
    690
    lilburne
    Nov 2, 2003
  2. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,074
  3. sinbad
    Replies:
    7
    Views:
    642
    sinbad
    Jun 19, 2008
  4. mlt
    Replies:
    2
    Views:
    817
    Jean-Marc Bourguet
    Jan 31, 2009
  5. Tobias Blass

    Can I put constant string arrays in a header file

    Tobias Blass, Apr 8, 2011, in forum: C Programming
    Replies:
    22
    Views:
    1,270
    Keith Thompson
    Apr 9, 2011
Loading...

Share This Page