简体   繁体   中英

C++ initialization of vector of structs

I am trying to make a keyword-recognizing subroutine under OSX Yosemite, see the listing below. I do have a couple of strange things.

I am using the "playground" for making MWE, and the project builds seemingly OK, but does not want to run: "My Mac runs OS X 10.10.5, which is lower than String sort's minimum deployment target." I do not understand even the message, and especially not what my code makes with sorting?

Then, I pasted the relevant code to my app, where the project was generated using CMake, and the same compiler, and the same IDE, in the same configuration presents with the message "Non-aggregate type 'vector cannot be initialized with an initializer list" in the "vector QInstructions={..}" construction.

When searching for similar error messages, I found several similar questions, and the suggested solutions use default constructor, manual initialization, and the like. I wonder if standard-resistant compact initialization is possible?

#include <iostream>
using namespace std;
#include <vector>

enum KeyCode {QNONE=-1,
    QKey1=100, QKey2
};

struct QKeys
{      /** The code command code*/
    std::string    Instr; ///< The command string
    unsigned int    Length; ///< The significant length
    KeyCode Code;  //
};

vector<QKeys> QInstructions={
{"QKey1",6,QKey1},
{"QKey2",5,QKey2}
};

KeyCode FindCode(string Key)
{
    unsigned index = (unsigned int)-1;
    for(unsigned int i=0; i<QInstructions.size(); i++)
        if(strncmp(Key.c_str(),QInstructions[i].Instr.c_str(),QInstructions[i].Length)==0)
        {
            index = i;
            cout << QInstructions[i].Instr << " " <<QInstructions[i].Length << " " << QInstructions[i].Code << endl;
            return QInstructions[i].Code;
            break;
        }
    return QNONE;
}

int main(int argc, const char * argv[]) {

    string Key = "QKey2";
    cout << FindCode(Key);
}

In your code

vector<QKeys> QInstructions={
("QKey1",6,QKey1),
{"QKey2",5,QKey2}
};

the first line of data is using parenthesis "()". Replace them with accolades "{}" and it will work.

Also, i see you have written unsigned index = (unsigned int)-1; . This is undefined behavior according to the standard. This is also bad because you are using a C-style cast (see here ). You should replace it with:

unsigned index = std::numeric_limits<unsigned int>::max();

Finally, I found the right solution as Initialize a vector of customizable structs within an header file . Unfortunately, replacing parenthesis did not help.

Concerning setting an unsigned int to its highest possible value using -1 , I find as overkill to use std::numeric_limits<unsigned int>::max() for such a case, a kind of over-standardization. I personally think that as long as we are using two's complement representation, the assignment will be correct. For example, at http://www.cplusplus.com/reference/string/string/npos/ you may read:

static const size_t npos = -1;

...

npos is a static member constant value with the greatest possible value for an element of type size_t.

...

This constant is defined with a value of -1, which because size_t is an unsigned integral type, it is the largest possible representable value for this type.

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