有人能告诉我如何在c++中解决这个问题吗?
错误是
不匹配'operator<<‘(操作数类型为'QList’和‘for 1*’) *this->list << form1;
myobj.h
#ifndef MYOBJ_H
#define MYOBJ_H
#include <QList>
#include "form.h"
#include "form1.h"
template <typename T>
class MyObj
{
public:
MyObj(QList<T*>* list)
{
this->list = list;
if (std::is_same<T,Form>::value) {
form = new Form;
*this->list << form;
}
if (std::is_same<T,Form1>::value) {
form1 = new Form1;
*this->list << form1;
}
}
QList<T*>* list = NULL;
Form* form = NULL;
Form1* form1 = NULL;
};
#endif // MYOBJ_Hmainwindow.cpp
myObj = new MyObj<Form>(new QList<Form*>);谢谢你对我的照顾
I编辑的
发布于 2016-05-02 22:49:38
我自己发答案,因为我找到了答案。这是否解决问题的正确方法?我对设计模式很感兴趣。
#ifndef MYOBJ_H
#define MYOBJ_H
#include <QList>
#include "form.h"
#include "form1.h"
#include <QDebug>
template <typename T>
class MyObj
{
public:
MyObj(QList<T*>* list)
{
this->list = list;
if (std::is_same<T,Form>::value) {
form = new Form;
appendFunc(form);
}
else if (std::is_same<T,Form1>::value) {
form1 = new Form1;
appendFunc(form1);
}
}
QList<T*>* list = NULL;
Form* form = NULL;
Form1* form1 = NULL;
template <typename U>
std::enable_if_t<std::is_same<T,U>::value,void>
appendFunc(U* a) {
qDebug() << "test100";
*this->list << a;
}
void appendFunc(...) { }
};
#endif // MYOBJ_H谢谢你对我的照顾
发布于 2016-05-03 14:36:40
为什么不直接使用模板参数呢?
template <typename T>
class MyObj
{
public:
MyObj()
{
list.push_back( new T );
}
private:
QList<T*> list;
};https://stackoverflow.com/questions/36992161
复制相似问题