Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QVector2D* unknown size

Tags:

c++

qt

My problem is about Qt compiler error: "QVector2D*: unknown size"

I've a Mesn class which holds some data for mesh definition like below.

class Mesh
{

public:

Mesh();
Mesh(const Mesh &other);
~Mesh();

private:

QVector<QVector3D> m_colors;
QVector<int> m_faces;
QVector<int> m_lines;
QVector<QVector3D> m_normals;
QVector<QVector2D> m_uvs;
QVector<QVector3D> m_vertices;
};

And in the cpp file I've this.

Mesh::Mesh()
{
m_colors   = QVector<QVector3D>();
m_faces    = QVector<int>();
m_lines    = QVector<int>();
m_normals  = QVector<QVector3D>();
m_uvs      = QVector<QVector2D>();
m_vertices = QVector<QVector3D>();
}

Mesh::Mesh(const Mesh &other)
{
m_colors   = other.GetColors();
m_faces    = other.GetFaces();
m_lines    = other.GetLines();
m_normals  = other.GetNormals();
m_uvs      = other.GetUVs();
m_vertices = other.GetVertices();
}

Mesh::~Mesh()
{

}

But I'm getting a compiler error I've mentioned about for only m_uvs. What is wrong with the code?

like image 521
Cahit Burak Küçüksütcü Avatar asked May 13 '26 04:05

Cahit Burak Küçüksütcü


1 Answers

  1. Your default constructor is not needed. The members will be default-constructed by the compiler-generated default constructor. Even if you had your own constructor, you don't need to default construct any non-POD members at all. POD members are plain old data types like int or bool[5]. Anything that is a class or a struct will be default constructed for you, so no need to do it explicitly.

  2. The copy constructor is not needed. The compiler will generate one for you. Same goes for the assignment operator and the destructor. That's the benefit of using properly designed C++ classes. You just use them, the compiler does all the boilerplate code generation for you.

  3. If you provide any of the big three: the destructor, the copy constructor, the assignment operator, you must provide all of them. When using C++11, the big three becomes the big four with addition of the move constructor.

  4. If you don't want the class to be copyable (you have a non-default destructor but you don't want to write the copy/move constructors nor the assignment operator), there's a Qt-provided macro for that:

    class MyClass {
      Q_DISABLE_COPY(MyClass)  // no semicolon!
      ...
    };
    
  5. You forgot to #include <QVector2D> in your .cpp file.

Incidentally, your code reads like Delphi/Borland Pascal code, where the compiler was of little help, compared to C++. In C++, if you use properly designed C++ classes as members of your class, you won't need to manually generate neither copy constructor nor the assignment operator, nor a destructor.

Essentially, your Mesh class can look as follows:

// header

class Mesh
{
  QVector<QVector3D> m_colors;
  QVector<int> m_faces;
  QVector<int> m_lines;
  QVector<QVector3D> m_normals;
  QVector<QVector2D> m_uvs;
  QVector<QVector3D> m_vertices;
public:
  Mesh();
  // You always need a const getter. The non-const getter is optional
  // if you allow modification of the member.
  QVector<QVector3D> & colors() { return m_colors; }
  const QVector<QVector3D> & colors() const { return m_colors; }
  // etc.
};

// implementation

Mesh::Mesh()
{}

// example

void test() {
  Mesh m;
  QVector<QVector3D> myColors;
  m.colors() = myColors; // works like a setter
  const Mesh cm;
  cm.colors() = myColors; // won't compile since cm is const
  QVector<QVector3D> colors = cm.colors(); // assigns to a local copy that can be changed
  colors << QVector3D(...);
}
like image 178
Kuba hasn't forgotten Monica Avatar answered May 15 '26 19:05

Kuba hasn't forgotten Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!