• 7232阅读
  • 9回复

[共享]怎么样让 Qmap 作为某个函数的参数 [复制链接]

上一主题 下一主题
离线fghfghfgh
 

只看楼主 倒序阅读 楼主  发表于: 2013-02-04
  1. 问题我已经解决了,正确代码在这个帖子里  希望可以帮到后来的同学
  2. 主要是后面我忘记写void  函数的返回类型
  3. http://www.qtcn.org/bbs/read.php?tid=52722&displayMode=1#139189







我使用的话 老是提示'QMap' is not a type
我也尝试了注册元类型  失败,可能是我操作方法不对我尝试了其他办法 也不行, 希望大婶可以指点谢谢


希望大神可以给出示例代码  谢谢



4条评分好评度+1贡献值+1金钱+1威望+1
jaelync 好评度 +1 - 2021-07-23
jaelync 贡献值 +1 - 2021-07-23
jaelync 威望 +1 - 2021-07-23
jaelync 金钱 +1 - 2021-07-23
离线XChinux

只看该作者 1楼 发表于: 2013-02-04
帖下你代码
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线fghfghfgh

只看该作者 2楼 发表于: 2013-02-04
  1. //TopAPI.h
  2. #ifndef TopAPI_H
  3. #define TopAPI_H
  4. #include <QString>
  5. #include <QMap>
  6. class TopAPI
  7. {
  8. public:
  9.     TopAPI();
  10.     ~TopAPI();
  11.     TopAPI(const QMap<QString, QString>& map);
  12. };


  1. //TopAPI.cpp
  2. #include "TopAPI.h"
  3. #include <QDebug>
  4. TopAPI::TopAPI(){
  5.      QMap<QString, QString> map;
  6.     TopAPI(map);
  7. }
  8. TopAPI::~TopAPI(){}
  9. TopAPI::TopAPI(QMap<QString, QString>& map){
  10.     QMapIterator<QString,QString> i(map);
  11.     while(i.hasNext()){
  12.          i.next();
  13.         qDebug()<< " " << i.key() << " "<<i.value();
  14.     }
  15. }





离线fghfghfgh

只看该作者 3楼 发表于: 2013-02-04
我是QT5.0
离线fghfghfgh

只看该作者 4楼 发表于: 2013-02-04
回 1楼(XChinux) 的帖子
贴上了
离线fghfghfgh

只看该作者 5楼 发表于: 2013-02-04
D:\My Documents\Qt5Pro\1\mainwindow.h:18: 错误:ISO C++ forbids declaration of 'TopAPI' with no type [-fpermissive]
离线XChinux

只看该作者 6楼 发表于: 2013-02-04
编译器是什么?
你上面的代码有问题啊。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线XChinux

只看该作者 7楼 发表于: 2013-02-04
.pro文件加C++11支持
  1. CONFIG += c++11


TopAPI.h文件
  1. #ifndef TopAPI_H
  2. #define TopAPI_H
  3. #include <QString>
  4. #include <QMap>
  5. class TopAPI
  6. {
  7. public:
  8.     TopAPI();
  9.     ~TopAPI();
  10.     TopAPI(const QMap<QString, QString> &map);
  11. };
  12. #endif


TopAPI.cpp文件
  1. //TopAPI.cpp
  2. #include "TopAPI.h"
  3. #include <QDebug>
  4. TopAPI::TopAPI() : TopAPI(QMap<QString,QString>())
  5. {
  6. }
  7. TopAPI::~TopAPI()
  8. {
  9. }
  10. TopAPI::TopAPI(const QMap<QString, QString>& map)
  11. {
  12.     QMapIterator<QString,QString> i(map);
  13.     while (i.hasNext())
  14.     {
  15.          i.next();
  16.         qDebug() << " " << i.key() << " " << i.value();
  17.     }
  18. }

二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线fghfghfgh

只看该作者 8楼 发表于: 2013-02-04
谢谢楼上的朋友帮忙,主要的问题出在我声明函数的时候 前面没有写 void 现在我把我正常的可以运行的的代码贴上来,希望可以帮到后来的朋友
  1. //mainwindow.h
  2. #ifndef MAINWINDOW_H
  3. #define MAINWINDOW_H
  4. #include <QMainWindow>
  5. #include <QString>
  6. #include <QMap>
  7. namespace Ui {
  8. class MainWindow;
  9. }
  10. class MainWindow : public QMainWindow
  11. {
  12.     Q_OBJECT
  13.     
  14. public:
  15.     explicit MainWindow(QWidget *parent = 0);
  16.     ~MainWindow();
  17.    void TopAPI(const QMap<QString, QString> &map);
  18.     
  19. private:
  20.     Ui::MainWindow *ui;
  21. };
  22. #endif // MAINWINDOW_H


  1. //mainwindow.cpp
  2. #include "mainwindow.h"
  3. #include "ui_mainwindow.h"
  4. #include <QDebug>
  5. MainWindow::MainWindow(QWidget *parent) :
  6.     QMainWindow(parent),
  7.     ui(new Ui::MainWindow)
  8. {
  9.     QMap<QString, QString> map;
  10.     map.insert("cfdsfdsf","c1");
  11.     map.insert("aaaab","d1");
  12.     map.insert("aaaac","e1");
  13.     map.insert("aaaad","a1");
  14.     map.insert("aaaaa","b1");
  15.     TopAPI(map);
  16.     ui->setupUi(this);
  17. }
  18. MainWindow::~MainWindow()
  19. {
  20.     delete ui;
  21. }
  22. void MainWindow::TopAPI(const QMap<QString, QString>& map){
  23.     QMapIterator<QString,QString> i(map);
  24.     while(i.hasNext()){
  25.          i.next();
  26.         qDebug()<< " " << i.key() << " "<<i.value();
  27.     }
  28. }




测试可以正常运行,我的QT5.1  编译器是mingw47_32


qt-windows-opensource-5.0.1-mingw47_32-x86-offline.exe
是我当时在本论坛下载的

离线zaq2010

只看该作者 9楼 发表于: 2013-02-05
后面的代码 后前面的代码完全没有关系。。。
说的不是一个东西。。
前面的是构造函数。。。
后面的是成员函数。。。
快速回复
限100 字节
 
上一个 下一个