Qt中的函数匹配
我在使用 Qt 时遇到了一些麻烦。
我在命名空间 Ui 中有一个类“Core”
class Core {
public:
static QString get_file_content(QString filename);
static void setMainwindow(Ui::MainWindow const *w);
private:
static MainWindow *main_window;
};
和类“MainWindow”:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
在 MainWindow 构造函数中,我犯了错误
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Core::setMainwindow(this);
}
并得到错误
mainwindow.cpp:8: error: no matching function for call to 'Core::setMainwindow(MainWindow* const)'
当然,我包含 core.h 和“Core”类的声明。
这种情况仅发生在 setMainwindow 方法上。
所以问题是 - 为什么 Core 类方法 setMainwindow() 在 MainWindow 类中不可见?
I have some trouble with Qt.
I have a class 'Core'
class Core {
public:
static QString get_file_content(QString filename);
static void setMainwindow(Ui::MainWindow const *w);
private:
static MainWindow *main_window;
};
and class 'MainWindow' in namespace Ui:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
In MainWindow constructor I make
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Core::setMainwindow(this);
}
and gets error
mainwindow.cpp:8: error: no matching function for call to 'Core::setMainwindow(MainWindow* const)'
Of cource, i include core.h with declaration of 'Core' class.
That's occurs only on setMainwindow method.
So the questions is - why Core class method setMainwindow() is invisible in MainWindow class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您的
Core::setMainwindow
方法采用Ui::MainWindow*
并且您正在传递MainWindow*
。从您发布的代码中,您有两个MainWindow
类,一个位于命名空间Ui
中,一个位于顶级命名空间中。这是你的意思吗?或者也许只有Ui
中有一个?The problem is that your
Core::setMainwindow
method takes aUi::MainWindow*
and you are passing aMainWindow*
. From the code you posted you have twoMainWindow
classes, one in the namespaceUi
and one in the top-level namespace. Is this what you mean or should there only be the one inUi
perhaps?您是否在 MainWindow 的 cpp/h 文件中添加了“Core.h”?
您是否尝试在 setMainWindow 中不带参数,只是为了检查它是否与它无关?
编辑:是的,在我看来,您需要 MainWindow 作为参数,而不是 Ui::MainWindow,您不觉得吗?
Did you add "Core.h" in your MainWindow's cpp/h file ?
Did you try without parameter in setMainWindow, just to check if it's not something related to it ?
Edit : Yeah seems to me you need MainWindow as parameter, not Ui::MainWindow, don't you think ?
您的
MainWindow
类未嵌套在Ui
命名空间内。您向前声明了Ui::MainWindow
类,但随后实现了一个单独的::MainWindow
类(在全局命名空间中)。因此,您的Core::setMainwindow
接受Ui::MainWindow
但您传递的是::MainWindow
。我猜这种缺乏嵌套的情况是正确的 - 并且
Ui::MainWindow
是由 Qt Designer 生成的,而MainWindow
是实现包含所有自定义代码的类。如果是这样,请将您的代码更改为:Your
MainWindow
class isn't nested inside theUi
namespace. You forward-declared theUi::MainWindow
class, but then implemented a separate::MainWindow
class (in the global namespace). Because of this, yourCore::setMainwindow
takes aUi::MainWindow
but you're passing a::MainWindow
.I'm guessing that this lack of nesting is correct -- and
Ui::MainWindow
is generated by Qt Designer, andMainWindow
is the implementation class that contains all of your custom code. If so, change your code to: