无法访问映射中初始化结构的内容

发布于 2024-10-17 10:51:58 字数 1646 浏览 4 评论 0原文

我有一个结构:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绿光 2024-10-24 10:51:58

我认为亨宁的答案是正确的选择。
代码中的 *gDial*gIP 表示 gDial[0]gIP[0]
因此,您只需将 KeyPair 数组的第一个元素插入到 mMasterList 中。

您的 pressKeyPairs 的声明
模板void pressKeyPairs(KeyPair(&keys)[nNumOfElements]);
本身是正确的。它将对 KeyPair 数组的引用作为参数。
但是,由于 mMasterListsecond_typeKeyPair(不是 KeyPair 数组),
pressKeyPairs((*it).second) 调用类型不匹配错误。

下面的想法怎么样?

  • 创建一个类型 KeyPairArray ,其中
    指向 KeyPair 数组
  • pressKeyPairs 引用
    KeyPairArray

例如:

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 );
  }
}

希望这会有所帮助。

I think Henning's answer is the way to go.
*gDial and *gIP in your code mean gDial[0] and gIP[0].
So, you insert only the first element of the KeyPair array into mMasterList.

Your pressKeyPairs's declaration
template<size_t nNumOfElements> void pressKeyPairs(KeyPair(&keys)[nNumOfElements]);
itself is correct. It takes a reference to KeyPair array as an argument.
However, since mMasterList's second_type is KeyPair(not KeyPair array),
pressKeyPairs((*it).second) invokes type mismatch error.

How about the following ideas?

  • Making a type KeyPairArray which
    points to KeyPair array
  • pressKeyPairs takes a reference to
    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.

没︽人懂的悲伤 2024-10-24 10:51:58

毕竟你所做的并没有那么错。您收到编译器错误只是因为编译器不知道如何将结构数组输出到 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)

×眷恋的温暖 2024-10-24 10:51:58

尝试在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文