简体   繁体   English

无法访问地图中初始化结构的内容

[英]Unable to access contents of initialized struct in a map

I have a struct: 我有一个结构:

typedef struct
{
    Qt::Key qKey;
    QString strFormType;
} KeyPair;

Now I initialize KeyPair instantiations so I could use it for my Automated Test App. 现在我初始化KeyPair实例化,以便我可以将它用于我的自动测试应用程序。

KeyPair gDial[] =
{
    { Qt::Key_1 , "MyForm" },
    { Qt::Key_1 , "SubForm" },
    { Qt::Key_Escape, "DesktopForm" }
};

KeyPair gIP[] =
{
    { Qt::Key_1 , "MyForm" },
    { Qt::Key_2 , "Dialog" },
    { Qt::Key_2 , "Dialog" },
    { Qt::Key_Escape, "DesktopForm" }
};
....
and like 100 more instantiations....

Currently, I call a function which uses these KeyPairs. 当前,我调用使用这些KeyPair的函数。

qDebug() << "Testing Test Menu"; 
pressKeyPairs( gDial);

qDebug() << "Testing Browse Menu"; 
pressKeyPairs( gIP);
....
and more calls like this for the rest... 

I would like to put all these KeyPair instantiations in a MAP so I wouldn't have to call pressKeyPairs() and qDebug() a hundred times... I'm a newbie in using MAPS... so I tried: 我想将所有这些KeyPair实例化放在MAP中,这样我就不必再调用pressKeyPairs()和qDebug()一百次......我是使用MAPS的新手......所以我试过:

map<string,KeyPair> mMasterList;
map<string,KeyPair>::iterator it;   

mMasterList.insert( pair<string, KeyPair>("Testing Test Menu", *gDial) ); //which I know is wrong, but how?
mMasterList.insert( pair<string, KeyPair>("Testing IP Menu", *gIP) );
mMasterList.insert( pair<string, KeyPair>("IP Menu2", *gIP2) );
....

for ( it=mMasterList.begin() ; it != mMasterList.end(); it++ )
{
   qDebug() << (*it).first << endl;
   pressKeyPairs((*it).second);       
   // I don't know how to access .second ... this causes a compiler error
}

EDIT: pressKeyPairs is declared as: 编辑:pressKeyPairs声明为:

template <size_t nNumOfElements> void pressKeyPairs(KeyPair (&keys)[nNumOfElements]); 

This code block isn't working... :( Can somebody tell me how to properly put these KeyPairs in a Map? 这个代码块不起作用...... :(有人能告诉我如何将这些KeyPairs正确放入Map中吗?

What you are doing is not that wrong after all. 你所做的事情毕竟不是那么错。 You are getting a compiler error just because the compiler does not know how to output your array of structs to cout, so if you just output (*it).first and iterate over the elements of (*it).second you should be fine. 您收到编译器错误只是因为编译器不知道如何将结构数组输出到cout,所以如果只输出(* it).first并遍历(* it).second的元素,那应该没问题。 Note however that you somehow need to make sure you know the number of entries in each of such arrays. 但是请注意,您需要以某种方式确保您知道每个此类数组中的条目数。 That can be achieved for example by always having some kind of null entry as the last one (or a convention that the escape key is always the last entry or whatever) 例如,可以通过始终将某种空条目作为最后一个条目来实现此目的(或者约定,转义键始终是最后一个条目或任何其他形式)

I think Henning's answer is the way to go. 我认为Henning的答案是要走的路。
*gDial and *gIP in your code mean gDial[0] and gIP[0] . *gDial代码中的*gDial*gIP表示gDial[0]gIP[0]
So, you insert only the first element of the KeyPair array into mMasterList . 因此,您只需将KeyPair数组的第一个元素插入mMasterList KeyPair

Your pressKeyPairs 's declaration template<size_t nNumOfElements> void pressKeyPairs(KeyPair(&keys)[nNumOfElements]); 您的pressKeyPairs的声明template<size_t nNumOfElements> void pressKeyPairs(KeyPair(&keys)[nNumOfElements]); itself is correct. 本身是正确的。 It takes a reference to KeyPair array as an argument. 它将KeyPair数组作为参数引用。
However, since mMasterList 's second_type is KeyPair (not KeyPair array), pressKeyPairs((*it).second) invokes type mismatch error. 但是,由于mMasterListsecond_typeKeyPair (不是KeyPair数组), pressKeyPairs((*it).second)调用类型不匹配错误。

How about the following ideas? 以下想法怎么样?

  • Making a type KeyPairArray which points to KeyPair array 创建一个指向KeyPair数组的KeyPairArray类型
  • pressKeyPairs takes a reference to KeyPairArray pressKeyPairs需要一参考KeyPairArray

For example: 例如:

struct KeyPairArray {
  size_t nNumOfElements;
  KeyPair *keys;

  template< size_t N >
  KeyPairArray( KeyPair(&k)[ N ] ) : nNumOfElements( N ), keys( k ) {}
};

// Example
void pressKeyPairs( KeyPairArray const& keys )
{
  for ( size_t i = 0;  i < keys.nNumOfElements;  ++ i ) {
    qDebug()<< keys.keys[ i ].qKey <<','<< keys.keys[ i ].strFormType <<'\n';
  }
}

int main() {
  map<string,KeyPairArray> mMasterList;
  map<string,KeyPairArray>::iterator it;
  mMasterList.insert(
    make_pair( "Testing Test Menu", KeyPairArray( gDial ) ) );

  for ( it=mMasterList.begin() ; it != mMasterList.end(); it++ ) {
    pressKeyPairs( it->second );
  }
}

Hope this helps. 希望这可以帮助。

Try to add Q_DECLARE_METATYPE(KeyPair) after your typedef declaration and also call qRegisterMetaType("KeyPair"); 尝试在typedef声明后添加Q_DECLARE_METATYPE(KeyPair) ,并调用qRegisterMetaType(“KeyPair”); before using KeyPair struct instances. 在使用KeyPair结构实例之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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