Batın Evirgen Batın Evirgen - 3 months ago 15
C++ Question

left of ".x" must have class/struct/union

I was implementing my cube class to Transform class. I am new opengl and c++.

left of '.x' must have class/struct/union

I got this error on every vertex definition on Cube.cpp -> InitBuffer();

I define my Transform constructor's in Transform.h header file with inline.

Transform.h

public:
Transform();
Transform(QVector3D pos, QQuaternion rot, QVector3D scale);

// Some Methods

QVector3D m_translation;
QQuaternion m_rotation;
QVector3D m_scale;
};
inline Transform::Transform() : m_translation(QVector3D(0.0f, 0.0f, 0.0f)), m_rotation(QQuaternion(0.0f, 0.0f, 0.0f, 0.0f)), m_scale(QVector3D(1.0f, 1.0f, 1.0f)) { }
inline Transform::Transform(QVector3D pos, QQuaternion rot, QVector3D scale) : m_translation(pos), m_rotation(rot), m_scale(scale) { }


Cube.h

public:
Cube();
Cube(QVector3D _position, QQuaternion _rotation, QVector3D _scale);

// Draw Method

Transform transform;

private:
void InitBuffer();

// Some Variables


Cube.cpp

Cube::Cube() {
transform = Transform(QVector3D(0.0f, 0.0f, 0.0f), QQuaternion(0.0f, 0.0f, 0.0f, 0.0f), QVector3D(1.0f, 1.0f, 1.0f));
buffersInitialized = false;
InitBuffer();
}

Cube::Cube(QVector3D _position, QQuaternion _rotation, QVector3D _scale) {
transform = Transform(_position, _rotation, _scale);
buffersInitialized = false;
InitBuffer();
}

void Cube::InitBuffer() {
GLfloat vertices[] = {
-transform.scale.x,-transform.scale.y,-transform.scale.z, // I got error in here
-transform.scale.x,-transform.scale.y, transform.scale.z,
-transform.scale.x, transform.scale.y, transform.scale.z,
transform.scale.x, transform.scale.y,-transform.scale.z,
-transform.scale.x,-transform.scale.y,-transform.scale.z,
-transform.scale.x, transform.scale.y,-transform.scale.z,
transform.scale.x,-transform.scale.y, transform.scale.z,
-transform.scale.x,-transform.scale.y,-transform.scale.z,
transform.scale.x,-transform.scale.y,-transform.scale.z,
transform.scale.x, transform.scale.y,-transform.scale.z,
.
.
.
.
};


Cube definition:

Cube c = Cube();

void GLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
c.Draw();
}

Answer

In Cube class, transform is a class member accessible throughout all that class. Note that transform is an instance of your Transform class.

Transform class have three class members:

  • QVector3D m_translation;
  • QQuaternion m_rotation;
  • QVector3D m_scale;

Therefore, accessing a class member of an instance looks like transform.m_scale not transform.scale, also note that transform.m_scale is of type QVector3D which have x(), y() and z() methods, so you would need the following code lines.

transform.m_scale.x() // instead of transform.scale.x
transform.m_scale.y() // instead of transform.scale.y
transform.m_scale.z() // instead of transform.scale.z