gpt4 book ai didi

c++ - "Cannot"移动指针

转载 作者:行者123 更新时间:2023-11-28 08:12:48 25 4
gpt4 key购买 nike

在我的代码中我有:

template<class Ui_Dialog>
QDialog* Base_Dialog<Ui_Dialog>::set_caller(QDialog *new_caller)
{
QDialog* old_caller = caller_;

caller_ = new_caller;//Here I'm trying to set this to new caller

return old_caller;
}

但是在将调用者设置为新调用者并从此 fnc 退出后,当我调用调用者时,我仍然得到旧调用者而不是新调用者,就好像没有进行任何更改一样。为什么?
编辑:

//caller is defined in a following way:  
class Main_Dialog : public Base_Dialog<Ui::Main_Dialog> {};

编辑 2:
有趣的是,如果我将 Base_Dialog 改为非模板并将 Main_Dialog 定义为:
类 Main_Dialog : public Base_Dialog, private Ui::Main_Dialog {};
然后它按预期工作。为什么?!
编辑 3:

class Main_Dialog;
template<class Ui_Dialog>
class Base_Dialog : public QDialog, protected Ui_Dialog
{
// Q_OBJECT //no signals/slots
protected:
Main_Dialog* main_dlg_;
QDialog* caller_;

public:
Base_Dialog(Main_Dialog *main_dlg, QDialog *caller, QWidget *parent = nullptr);
QDialog* set_caller(QDialog *);
QDialog* clear_caller();
Main_Dialog* clear_main_dlg();

};

/*----------------*/

#include "Main_Dialog.hpp"
template<class Ui_Dialog>
Base_Dialog<Ui_Dialog>::Base_Dialog(Main_Dialog *main_dlg,QDialog *caller, QWidget *parent):
QDialog(parent),
main_dlg_(main_dlg),
caller_(caller)
{
//setupUi(this);
}


#include <QtDebug>
template<class Ui_Dialog>
QDialog* Base_Dialog<Ui_Dialog>::set_caller(QDialog *new_caller)
{
QDialog* old_caller = caller_;

caller_ = new_caller;

return old_caller;
}
#include "_1Dialog.hpp"
#include "_2Dialog.hpp"
class Main_Dialog : public Base_Dialog<Ui::Main_Dialog>
{
Q_OBJECT
QSet<QDialog*>* dialogs_;
private:
template<class Dialog,class Caller>
bool already_created_(Caller*&, QDialog*& already_exists)const;

template<class Dialog,class Caller, class Parent>
QDialog* create_(Caller*,Parent*);

/*template<class Dialog>
void show_();*/

/* template<class Dialog,class Caller>
QDialog* find_(Caller*)const;*/
public:
explicit Main_Dialog(QWidget *parent = 0);
template<class Dialog,class Caller>
QDialog* get_dialog(Caller*& caller);
public slots:
void _1clicked();
void _2clicked();
};

template<class Dialog,class Caller>
bool Main_Dialog::already_created_(Caller*& caller,QDialog*& already_exists)const
{/*the already_exists is introduced here in order to remove repetions of code and
searching*/
auto beg = dialogs_->begin();
auto end = dialogs_->end();
while(beg != end)
{
if(dynamic_cast<Dialog*>(*beg))
{
already_exists = *beg;
static_cast<Base_Dialog*>(already_exists)->set_caller(caller);
return true;
}

++beg;
}
return false;
}



template<class Dialog,class Caller, class Parent>
QDialog* Main_Dialog::create_(Caller *caller, Parent *parent)
{
return (*dialogs_->insert(new Dialog(this,caller,parent)));
}

template<class Dialog,class Caller>
QDialog* Main_Dialog::get_dialog(Caller *&caller)
{
QDialog* already_exists = nullptr;
if (already_created_<Dialog>(caller,already_exists))
{
return already_exists;
}
else
{
return create_<Dialog>(caller,this);
}
#include "Base_Dialog.hpp"
#include "ui__1Dialog.h"

class Main_Dialog;
class _1Dialog : public Base_Dialog<Ui::_1Dialog>
{
Q_OBJECT
public:
explicit _1Dialog(Main_Dialog* main_dlg, QDialog*caller, QWidget *parent = 0);

private slots:
void _2clicked();
void caller_clicked();
void main_clicked();
};

#endif // _1DIALOG_HPP
#include "_1Dialog.hpp"
#include "_2Dialog.hpp"
#include "Main_Dialog.hpp"
_1Dialog::_1Dialog(Main_Dialog* main_dlg, QDialog*caller, QWidget *parent) :
Base_Dialog(main_dlg,caller,parent)
{
setupUi(this);
}

void _1Dialog::_2clicked()
{
this->hide();
main_dlg_->get_dialog<_2Dialog>(this)->show();
}


void _1Dialog::caller_clicked()
{
this->hide();
caller_->show();
}

void _1Dialog::main_clicked()
{
this->hide();
main_dlg_->show();
}

}
#endif // MAIN_DIALOG_HPP

最佳答案

我认为您的问题不在 set_caller 中。接下来对我有用:

#include <iostream>

using namespace std;

class QDialog {
int i;
public:
QDialog() : i(0) {}
QDialog(int i) : i(i) {}
void me() { cout << i << endl; }
};

namespace UI {
class Main_Dialog {}; // EDITED
}

template<class Ui_Dialog>
class Base_Dialog : public QDialog, protected Ui_Dialog
{
QDialog* caller_;
public:
QDialog* set_caller(QDialog *new_caller);
QDialog* get_caller() {return caller_; } ;
};

template<class Ui_Dialog>
QDialog* Base_Dialog< Ui_Dialog>::set_caller(QDialog *new_caller)
{
QDialog* old_caller = caller_;

caller_ = new_caller;//Here I'm trying to set this to new caller

return old_caller;
}

class Main_Dialog : public Base_Dialog<UI::Main_Dialog> {
public:
};

int main()
{
QDialog q1(1);
QDialog q2(2);

Main_Dialog md;

md.set_caller(&q1);
md.get_caller()->me();
md.set_caller(&q2);
md.get_caller()->me();


return 0;
}

关于c++ - "Cannot"移动指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8558276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com