无法访问映射中初始化结构的内容
我有一个结构:
typedef struct
{
Qt::Key qKey;
QString strFormType;
} KeyPair;
现在我初始化 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....
目前,我调用一个使用这些密钥对的函数。
qDebug() << "Testing Test Menu";
pressKeyPairs( gDial);
qDebug() << "Testing Browse Menu";
pressKeyPairs( gIP);
....
and more calls like this for the rest...
我想将所有这些 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
}
编辑: pressKeyPairs 声明为:
template <size_t nNumOfElements> void pressKeyPairs(KeyPair (&keys)[nNumOfElements]);
此代码块不起作用... :( 有人可以告诉我如何正确地将这些 KeyPairs 放入 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 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.
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:
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为亨宁的答案是正确的选择。
代码中的
*gDial
和*gIP
表示gDial[0]
和gIP[0]
。因此,您只需将
KeyPair
数组的第一个元素插入到mMasterList
中。您的
pressKeyPairs
的声明模板void pressKeyPairs(KeyPair(&keys)[nNumOfElements]);
本身是正确的。它将对
KeyPair
数组的引用作为参数。但是,由于
mMasterList
的second_type
是KeyPair
(不是KeyPair
数组),pressKeyPairs((*it).second)
调用类型不匹配错误。下面的想法怎么样?
KeyPairArray
,其中指向
KeyPair
数组pressKeyPairs
引用KeyPairArray
例如:
希望这会有所帮助。
I think Henning's answer is the way to go.
*gDial
and*gIP
in your code meangDial[0]
andgIP[0]
.So, you insert only the first element of the
KeyPair
array intomMasterList
.Your
pressKeyPairs
's declarationtemplate<size_t nNumOfElements> void pressKeyPairs(KeyPair(&keys)[nNumOfElements]);
itself is correct. It takes a reference to
KeyPair
array as an argument.However, since
mMasterList
'ssecond_type
isKeyPair
(notKeyPair
array),pressKeyPairs((*it).second)
invokes type mismatch error.How about the following ideas?
KeyPairArray
whichpoints to
KeyPair
arraypressKeyPairs
takes a reference toKeyPairArray
For example:
Hope this helps.
毕竟你所做的并没有那么错。您收到编译器错误只是因为编译器不知道如何将结构数组输出到 cout,因此如果您只输出 (*it).first 并迭代 (*it).second 的元素,您应该没问题。但请注意,您需要以某种方式确保您知道每个此类数组中的条目数。例如,可以通过始终将某种空条目作为最后一个条目来实现(或者转义键始终是最后一个条目或其他任何约定)
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. 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)
尝试在 typedef 声明后添加 Q_DECLARE_METATYPE(KeyPair) ,并在使用 KeyPair 结构体实例之前调用 qRegisterMetaType("KeyPair"); 。
Try to add Q_DECLARE_METATYPE(KeyPair) after your typedef declaration and also call qRegisterMetaType("KeyPair"); before using KeyPair struct instances.