我的问题与Qt编译器错误有关:“QVector2D *:未知大小”
我有一个Mesn类,其中包含一些用于网格定义的数据,如下所示。
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;
};
在cpp文件中,我已经拥有了。
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()
{
}
但是我遇到了仅针对m_uvs提到的编译器错误。代码有什么问题?
最佳答案
int
或bool[5]
。任何属于类或结构的东西都将默认为您构造,因此无需显式地执行。 class MyClass {
Q_DISABLE_COPY(MyClass) // no semicolon!
...
};
#include <QVector2D>
。 顺便说一句,您的代码读起来就像Delphi / Borland Pascal代码一样,与C++相比,编译器几乎没有帮助。在C++中,如果您使用正确设计的C++类作为类的成员,则无需手动生成复制构造函数,赋值运算符或析构函数。
本质上,您的
Mesh
类可以如下所示:// 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(...);
}