C++ 从相互包含的文件中分配类的对象数组时,如何解决ssertion失败?

C++ 从相互包含的文件中分配类的对象数组时,如何解决ssertion失败?,c++,assertion,C++,Assertion,我有两个类Mat3和Vec3,放在不同的文件中。Mat3由Vec3数组组成,Vec3在某些计算中使用Mat3,因此这些文件相互包含。我将它们的原型放在标题中,但在Mat3中这是有问题的。由于类型不完整,Vec3的数组无法编译,所以我将其更改为Vec3*,并计划在构造函数中分配内存,但使用new或malloc时断言失败,我知道new使用malloc,只是指出我使用的组合如下: Mapper:malloc.c:2379:sysmalloc:Assertion`old_top==initial_top

我有两个类Mat3和Vec3,放在不同的文件中。Mat3由Vec3数组组成,Vec3在某些计算中使用Mat3,因此这些文件相互包含。我将它们的原型放在标题中,但在Mat3中这是有问题的。由于类型不完整,Vec3的数组无法编译,所以我将其更改为Vec3*,并计划在构造函数中分配内存,但使用new或malloc时断言失败,我知道new使用malloc,只是指出我使用的组合如下:

Mapper:malloc.c:2379:sysmalloc:Assertion`old_top==initial_top av&&old_size==0 | | |无符号long old_size>=MINSIZE&&prev_inuse old_top&&unsigned long old_end&pagesize-1==0'失败

所以现在我不知道如何管理所有这些。你能帮忙吗

为了更好地理解,我将简化课程放在下面:

---------Vec3.h-----------
#ifndef VEC3_H
#define VEC3_H
#include <cmath>
#include <assert.h>
#include "Vec2.h"
#include "Mat3.h"
namespace API
{
    namespace Data
    {
        struct Mat3;
        struct Vec3
        {
            float X = 0.0f;
            float Y = 0.0f;
            float Z = 0.0f;
            Vec3();
            Vec3(float, float, float);
            Vec3(Vec3*);
            void Rotate(const Vec3);
        };
    }
}
#endif

编辑:代码已更新为最低可编译版本。

这对我很有用:

Vec3.h

Mat3.h

Mat3.cpp

在C/C++中,当您使用交叉引用系统时,您总是在标题中提出类的前向声明,而包含此类的标题是没有用的。 . 因此,在.cpp文件中,您应该包含标题以包含类的定义,而不仅仅是声明


另一件事:如果在构造函数中使用new[]在堆上分配数据,则应使用delete[]在析构函数中删除该数据。

让类彼此包含是个坏主意,请阅读:请发布一个最小编译示例。@Superlokkus完成。
---------Vec3.cpp-----------
#include "Vec3.h"

API::Data::Vec3::Vec3()
{
}

API::Data::Vec3::Vec3(float x, float y, float z)
{
    X = x;
    Y = y;
    Z = z;
}

API::Data::Vec3::Vec3(Vec3* original) : API::Data::Vec3::Vec3(original->X, original->Y, original->Z)
{
}

void API::Data::Vec3::Rotate(const API::Data::Vec3 angles)
{
    Mat3 rotationMatrix = Mat3(0.0f);
    Mat3 xRotation = Mat3(0.0f);
    Mat3 yRotation = Mat3(0.0f);
    Mat3 zRotation = Mat3(0.0f);
    xRotation.XRotation(angles.X);
    ///
    yRotation.YRotation(angles.Y);
    ///
    zRotation.ZRotation(angles.Z);

    rotationMatrix = xRotation * yRotation * zRotation;
    Vec3 effect = rotationMatrix * (*this);
    X = effect.X;
    Y = effect.Y;
    Z = effect.Z;
}
-------Mat3.h------------
#ifndef MAT3_H
#define MAT3_H
#include <vector>
#include <assert.h>
#include "Vec3.h"
namespace API
{
    namespace Data
    {
        struct Vec3;
        struct Mat3
        {
        private:
            Vec3* _columns = nullptr;
        public:
            Mat3();
            Mat3(const Mat3&);
            Mat3(float);
        };
    }
}
#endif // !MAT3_H

-------Mat3.cpp-------
API::Data::Mat3::Mat3(float value)
{
    _columns = new API::Data::Vec3[3]; // Assertion fail fires here
    //OR
    // _columns = (API::Data::Vec3*)malloc(3 * sizeof(API::Data::Vec3));
    for (int i = 0; i < 3; i++)
    {
        _columns[i] = API::Data::Vec3();
        for (int j = 0; j < 3; j++)
        {
            _columns[i][j] = value;
        }
    }
}

API::Data::Mat3::Mat3() : API::Data::Mat3(0.0f)
{
}

API::Data::Mat3::Mat3(const API::Data::Mat3& original) : API::Data::Mat3(0.0f)
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            _columns[i][j]= original[i][j];
        }
    }
}
#ifndef VEC3_H
#define VEC3_H
#include <cmath>
#include <assert.h>
namespace API
{
    namespace Data
    {
        struct Mat3;//forward declaration
        struct Vec3
        {
            float X = 0.0f;
            float Y = 0.0f;
            float Z = 0.0f;
            Vec3();
            Vec3(float, float, float);
            Vec3(Vec3*);
            void Rotate(const Vec3);
        };
    }
}
#endif
#include "Vec3.h"
#include "Mat3.h"

API::Data::Vec3::Vec3()
{
}

API::Data::Vec3::Vec3(float x, float y, float z)
{
    X = x;
    Y = y;
    Z = z;
}

API::Data::Vec3::Vec3(Vec3* original) : API::Data::Vec3::Vec3(original->X, original->Y, original->Z)
{
}

void API::Data::Vec3::Rotate(const API::Data::Vec3 angles)
{
    Mat3 rotationMatrix = Mat3(0.0f);
    Mat3 xRotation = Mat3(0.0f);
    Mat3 yRotation = Mat3(0.0f);
    Mat3 zRotation = Mat3(0.0f);
    //Comments because you have not implemented rotations for instance
    /*xRotation.XRotation(angles.X);
    ///
    yRotation.YRotation(angles.Y);
    ///
    zRotation.ZRotation(angles.Z);

    rotationMatrix = xRotation * yRotation * zRotation;
    Vec3 effect = rotationMatrix * (*this);
    X = effect.X;
    Y = effect.Y;
    Z = effect.Z;*/
}
#ifndef MAT3_H
#define MAT3_H
#include <assert.h>
namespace API
{
    namespace Data
    {
        struct Vec3;//forward declaration
        struct Mat3
        {
        private:
            Vec3* _columns = nullptr;
        public:
            Mat3();
            Mat3(const Mat3&);
            Mat3(float);
            ~Mat3();
        };
    }
}
#endif // !MAT3_H
#include "Mat3.h"
#include "Vec3.h"


API::Data::Mat3::Mat3(float value)
{
    _columns = new API::Data::Vec3[3];
    assert(_columns != 0);

    for (int i = 0; i < 3; i++)
    {
        _columns[i] = API::Data::Vec3();
        _columns[i].X = value;
        _columns[i].Y = value;
        _columns[i].Z = value;
    }
}

API::Data::Mat3::Mat3() : API::Data::Mat3(0.0f)
{
}

API::Data::Mat3::Mat3(const API::Data::Mat3& original) : API::Data::Mat3(0.0f)
{
    for (int i = 0; i < 3; i++)
    {

        _columns[i].X = original._columns[i].X;
        _columns[i].Y = original._columns[i].Y;
        _columns[i].Z = original._columns[i].Z;
    }
}


API::Data::Mat3::~Mat3() {
    delete[] _columns;
}