调试断言失败!表达式:_BLOCK_TYPE_IS_VALID

8 c++ debugging assertions

我收到此错误消息:

调试断言失败!

表达:_BLOCK_TYPE_US_VALID(pHead-> nBlockUse)

同时尝试做以下事情

#include <vector>
#include <algorithm>
using namespace std;

class NN
{
public:
    NN(const int numLayers,const int *lSz,const int AFT,const int OAF,const double initWtMag,const int UEW,const double *extInitWt);
    double sse;
    bool operator < (const NN &net) const {return sse < net.sse;}
};

class Pop
{
    int popSize;
    double a;
public:

    Pop(const int numLayers,const int *lSz,const int AFT,const int OAF,const double initWtMag,const int numNets,const double alpha);
    ~Pop();
    vector<NN> nets;
    void GA(...);
};

Pop::Pop(const int numLayers,const int *lSz,const int AFT,const int OAF,
         const double initWtMag,const int numNets,const double alpha)
{
    popSize=numNets;
    a=alpha;
    nets.reserve(popSize);
    for(int i=0;i<popSize;i++)
    {
        NN *net = new NN (numLayers,lSz,AFT,OAF,initWtMag,0,0);
        nets.push_back(*net);
    }
}

void Pop::GA()
{
...
        sort(nets.begin(),nets.end());
...
}
Run Code Online (Sandbox Code Playgroud)

该错误似乎与排序功能有关.我检查网络矢量的所有实例,它们似乎没问题,有不同的sse.有趣的是,我创建了一个更简单的上述代码案例(见下文),它没有任何错误.我正在破坏我的大脑.请帮忙.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

class Student
{
public:
    string name;
    double grade;
    Student(string,double);
    bool operator < (const Student &st) const {return grade < st.grade;}
};

Student::Student(string stName,double stGrade)
{
    name = stName;
    grade = stGrade;
}

int main()
{
    vector<Student> group;
    Student *st;
    st = new Student("Bill",3.5);
    group.push_back(*st);
    st = new Student("John",3.9);
    group.push_back(*st);
    st = new Student("Dave",3.1);
    group.push_back(*st);
    sort(group.begin(),group.end());
    for each(Student st in group)
        cout << st.name << " " << st.grade << endl;
    cin.get();
    return(0);
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*her 11

当您覆盖由分配的块的标头时,_BLOCK_TYPE_IS_VALID断言被触发new.切片对象,使用死对象等时会发生这种情况.

您应该查看完整的代码,并尝试使用调试器中的数据.这个简短的代码片段包含了C++的一些"好奇"用法,但没有明显的点可以产生所描述的错误(至少对我而言).