English 中文(简体)
(C++) QT. QList 只允许使用固定等级物体?
原标题:(C++ QT) QList only allows appending constant class objects?
  • 时间:2010-01-07 22:32:51
  •  标签:
  • c++
  • qt

I m 粗略新到QT。 现在,我与我在一起一周。 我在试图把一种习俗数据类添加到像这样的清单中时,遇到了一个错误。

QObject parent;

QList<MyInt*> myintarray;

myintarray.append(new const MyInt(1,"intvar1",&parent));
myintarray.append(new const MyInt(2,"intvar2",&parent));
myintarray.append(new const MyInt(3,"intvar3",&parent));

我的门别类是一只简单的 wrap子,看像这样的东西。

#ifndef MYINT_H
#define MYINT_H

#include <QString>
#include <QObject>

class MyInt : public QObject
{ 
 Q_OBJECT

 public:

MyInt(const QString name=0, QObject *parent = 0);
MyInt(const int &value,const QString name=0, QObject *parent = 0);
MyInt(const MyInt &value,const QString name=0,QObject *parent = 0);
int getInt() const;

public slots:
void setInt(const int &value);
void setInt(const MyInt &value);

signals:
 void valueChanged(const int newValue);

private:
int intStore;

};

#endif

清单附录中的错误一米

error: invalid conversion from const MyInt* to MyInt* error:
initializing argument 1 of void QList::append(const T&) [with T = MyInt*]

如果任何人能够指出什么是错的,那将是很麻烦的。

最佳回答

因此,你制定了一份清单:

<编码> QList<MyInt*> myintarray;

然后,你试图传讯。

myintarray.append(new const MyInt(1,"intvar1",&parent));

问题在于,MyInt正在形成一个神学院。

你们要么需要改变你的QList,以便像这样的老板:

<编码> QList<const MyInt*> myintarray;

或您不需要通过改变您的口号来建立最灵敏星号:

myintarray.append(new MyInt(1,"intvar1",&parent));

你选择的方法将完全取决于你如何使用你的QList。 如果你永远不想改变我的Int中的数据,你只想把我的Int混为一谈。

问题回答

你们需要使用:

   QList<const MyInt*> myintarray;

The compiler is telling you all you need to now - you re trying to store a const T* as a T* and implicit conversions from const T* to T* are not allowed.
Just leave out the const when append()ing.

我要说的是,你只是通过MyInt* 定期进入QList:上诉。 “const T&”指点型QList不会重新指定你提供的点人。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签