简体   繁体   中英

Initializing a static const vector of vectors in Visual Studio 2012

I am trying to create a static const vector of const vectors of ints (there's gotta be a better way to do this) in Visual Studio 2012 and I can't figure out the proper syntax to initialize it with. I believe 2012 uses a version of C++ that doesn't allow initializers but I don't know how else to accomplish what I want.

I've tried the following in 2013, and it seems to compile ok:

.h:

static const std::vector<const std::vector<int>> PartLibrary;

.cpp:

const std::vector<const std::vector<int>> Parts::PartLibrary {
    std::vector<int> { 29434 }, // 1
    std::vector<int> { 26322 }, // 2
...
}

However, when I try the same in 2012, it errors out:

Error   1   error C2470: 'PartLibrary' : looks like a function definition, 
but there is no parameter list; skipping apparent body

How can I properly initialize this? Is there a more appropriate data type out there I can use? I simply want my static class to have a constant vector of vectors of ints so I can quickly read, but not modify, values.

In C++, you can't have a std::vector< const anything>, see for example here . The elements have to be Assignable.

In C++98, you could try the following initialization scheme. It has the disadvantage of copying the vectors from the array to the vector:

const std::vector<int> vectors[2] = {
    std::vector<int> (1, 29434), // vector of one element
    std::vector<int> (1, 26322), // vector of one element
};
const std::vector<std::vector<int> > /*Parts::*/PartLibrary (vectors+0, vectors+2);
// space needed for C++98---------^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM