CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    669

    const vector<vector<int> > ??

    I'd like to define a constant global integer array 3 x N (N is known at compile time, just not off the top of my head), but I'm shying away from using standard arrays, such as int[3][N], since you can drop off the edge of them when not careful. I'm thinking about using vector<vector<int> >, but I'm having some noobie trouble initializing it. Can anyone give me some pointers to how I might define a constant global vector and initialize it in, say, GlobalVariables.h file?

    For starters, I've tried:
    vector<int> tempvec;
    tempvec.push_back( /*Number of integers*/ 1);
    tempvec.push_back( /*Number of doubles*/ 0);
    tempvec.push_back( /*Number of booleans*/ 0);
    static const vector<int> NUMBERS_IN_STONE(tempvec);

    . . . but that doesn't work, presumably because it's in an .h file.

    I'd like to find something like:
    static const vector<vector<int> > YeOlNeverChangingNumbers( ??initialization list?? ) and plop that into the .h file, but I can't find anything that works.

    Is there a better strategy that I might take?

    I'm basically looking for a constant global array with some border-patrol built in. Thanks guys.

  2. #2
    Join Date
    Feb 2003
    Posts
    377

    Re: const vector<vector<int> > ??

    The vector class is a good way to go, although you can fall off the edge of a vector also. Here is an example of a two dimensional vector with 3 rows and 8 columns (I'm just randomly choosing N = 8, you can fill in whatever number):
    Code:
    std::vector<std::vector<int> > vec(3, std::vector<int>(8, -1));
    That code initializes all 24 elements to -1. You can put whatever value you want there, and if you leave out the -1 then it will default to 0.

    If you want different values for each row, then you will need some sort of initialization routine to do it. One option is keeping track of whether the vector was initialized and the first time you use it call the initialization method. Another option is to encapsulate it into a class and make a global instance of the class instead of the vector itself, that way the constructor can initialize each row separately. Yet another option is to have three separate vectors. All these options assume you want to initialize with different values, otherwise the code above will be fine.

  3. #3
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    669

    Re: const vector<vector<int> > ??

    I seem to be having trouble getting a global vector to work, period. I have the following in a GlobalVariables.h file:

    Code:
    extern vector<int> globalvec;
    and the definition in GlobalVariables.cpp file:

    Code:
    #include <vector>
    using namespace std;
    
    vector<int> globalvec;
    globalvec.push_back(1);
    globalvec.push_back(0);
    globalvec.push_back(0);
    But when I compile it, I get the following errors . . .
    error C2143: syntax error : missing ';' before '.'
    error C2501: 'globalvec' : missing storage-class or type specifiers
    error C2371: 'globalvec' : redefinition; different basic types
    see declaration of 'globalvec'

    I don't see how it could have been redefined. I didn't #include GlobalVariables.h into GlobalVariables.cpp. The whole point of GlobalVariables.h is to #include it elsewhere to let everything else know that globalvec exists. It turns out, if I write:

    Code:
    vector<int> anyname;
    anyname.push_back(1);
    anyname.push_back(0);
    anyname.push_back(0);
    I get the same relative error messages.

    What is going on here? Isn't there any easy way to declare a global variable??

  4. #4
    Join Date
    Feb 2003
    Posts
    377

    Re: const vector<vector<int> > ??

    One problem is that you are calling push_back outside of a function. You can't just have code lying around by itself. The declaration and definition of the vector are ok, and it is ok to use a constructor when you define the global variable, but if you want to call a member function you have to do it inside some other function. That is why I suggested an initialization routine of some sort - either a standalone function that you call at the beginning of the program, or a class that encapsulates the vector and initializes it in a constructor, or some other method of pushing back the data.

    Since you want it to be const (which I didn't notice until now) you can write a function that makes a non-const vector on the stack with the data you want, then use that to initialize the const global vector. Or you could use a regular C style array to initialize the vector. An example of the first idea:
    Code:
    vector<vector<int> > CreateInitialVector()
    {
        vector<int> temp;
        temp.push_back(1);
        temp.push_back(2);
        temp.push_back(0);
        temp.push_back(6);
        return vector<vector<int> >(3, temp);
    }
    
    vector<vector<int> > globalVec = CreateInitialVector();

  5. #5
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    669

    Re: const vector<vector<int> > ??

    Hmmm. I wasn't aware that you had to have it in a function, and that does clear up some confusion. Thank you for the info. I'll get to work on it.

  6. #6
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    669

    Re: const vector<vector<int> > ??

    I still can't seem to cover my goal here.

    What I'd like to find is a way to define a static const container member of a class in the following way:

    Code:
    const container_type<int> MyClass::MyClassContainer =
    {
    { 3, 8, 2},
    {4, 9 ,2}
    }
    It seems like static members have to be initialized all in one go, which makes it difficult to push_back anything into a vector.

    The other option is to declare a const static integer array, which is easily initialized as above, and then declare two other const static integers: NUMBER_OF_COLUMNS and NUMBER_OF_ROWS. This starts to make things particularly messy, and creates supreme chaos in my program if NUMBER_OF_ROWS doesn't actually match the number of rows, (and the same for columns). I'm particularly worried about some witless programmer coming along and not being aware of this problem . . . like, say, me in a year. I can testify that I'm thoroughly witless afterall.

    So, if there is indeed no means of intializing some type of container as above and I am forced to go by this second route, is there some way I can package it into a nice little macro that the compiler can unpackage as necessary? An example might be:

    CREATE_ARRAY_INITIALIZATION(MyClass, MyClassContainer, { { 3, 8, 2}, {4, 9 ,2} })

    If designed correctly, the macro can count the number of rows and columns, thereby making sure that NUMBER_OF_ROWS and NUMBER_OF_COLUMNS is correct and circumventing the fact that I am completely witless.

    It should be pointed out that I'm looking for a macro that does not physically replace the above notation with code; the macro should encourage the compiler to interpret the notation as the equivalent appropriate code. Is that possible??

    Any sort of idea would be much appreciated! Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured