这个对我来说有点奇怪。
我有一个指向类成员 ( mGeometry
) 的指针,它又持有一个指向 QList< GLushort >
的指针。数据成员 ( mFaces
)。我正在尝试通过类 Cube
获取 mFaces 的索引.
所以,更简化的版本看起来像这样:
struct Geometry
{
Geometry( void );
~Geometry( void );
void someFunc( void );
QList< GLushort > *mFaces;
};
class Cube
{
public:
Cube( void );
~Cube( void );
void anotherFunc( void );
Geometry *mGeometry;
};
假设,在 anotherFunc
,我们正在尝试执行以下操作:
GLushort *indeces = new GLushort;
*indeces = ( *mGeometry ).mFaces[ 0 ];
错误
error: cannot convert ‘QList<short unsigned int>’ to ‘GLushort {aka short unsigned int}’ in assignment
所以,我们尝试:
*indeces = mGeometry->( *mFaces )[ 0 ]; //which, is originally how I've accessed pointers-to-containers' indexes.
错误
error: expected unqualified-id before ‘(’ token
error: ‘mFaces’ was not declared in this scope
当然,显而易见的是:
*indeces = mGeometry->mFaces[ 0 ];
错误
error: cannot convert ‘QList<short unsigned int>’ to ‘GLushort {aka short unsigned int}’ in assignment
几何构造函数
Geometry::Geometry( void )
: mFaces( new QList< GLushort > )
{
}
我在这里做错了什么吗?如果不是,获取指向 mFaces
的指针索引的正确方法是什么? ?
最佳答案
因为 mFaces
是一个指针,你必须取消引用 mGeometry
与 ->
然后取消引用 mFaces
与 *
然后使用 QList<>
的 operator[]
获取号码:
*indeces = (*mGeometry->mFaces)[0]; // note that * has lower precedence than ->
// so this is like (*(mGeometry->mFaces))[0]
有点奇怪因为[0]
与 *
做同样的事情.指针类型的索引,例如 x[i]
,遵循公式 *(x + i)
,因此您也可以这样做以获得相同的效果(但不要这样做):
*indeces = mGeometry->mFaces[0][0]; // or *indeces = (*mGeometry).mFaces[0][0];
这与 (*(mFaces + 0))[0]
相同, 这与 (*mFaces)[0]
完全相同.
这也是您收到错误 cannot convert ‘QList<short unsigned int>’ to ‘GLushort’
的原因当你试图做的时候
( *mGeometry ).mFaces[ 0 ];
因为 ( *mGeometry ).mFaces[ 0 ];
(同样,相当于上面的 *mGeometry->mFaces
)让你得到一个 QList<GLushort>
, 你必须使用 operator[]
的 QList<>
获取您的数据。
现在对于一些完全不相关的东西,你拼错了 indices :)
关于c++ - 获取另一个指针类成员封装的指针类成员列表的索引值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8947384/