nihaomaccc 2016-12-06 10:30 采纳率: 0%
浏览 1264
已结题

C++ VECTOR push_back 出现stack_overflow

#include
#include
#include
using namespace std;

class CMyClass
{
public:
CMyClass() {}
CMyClass(int a)
{
this->m_nValue = a;
vec.push_back(*this);
}
CMyClass(const CMyClass&c) {
m_nValue = c.m_nValue;
}
~CMyClass()
{
/*不知道怎么指向当前类*/
vector::iterator it;
for (it = vec.begin(); it != vec.end(); it++)
{
if (it->vec == *this)
vec.erase(it);
}

}
friend bool operator==(vector<CMyClass>v, CMyClass& c2)
{
    int length = v.size();
    for (int i = 0; i < length; i++)
    {
        if (&v[i] == &c2)
            return true;
    }
    return false;
}
friend ostream& operator<<(ostream& out, CMyClass& c)
{
    out << c.m_nValue << endl;
    return out;
}
static void ShowList()
{
    vector<CMyClass>::iterator it;
    for (it = vec.begin(); it != vec.end(); it++)
    {
        cout << *it;

    }
}

private:
int m_nValue;
static vectorvec;
};

vectorCMyClass::vec;

int main()
{
CMyClass a(100);
CMyClass b(1);
CMyClass c;
CMyClass* d = new CMyClass;
*d = 10;
cout << "The first end:" << endl;
CMyClass::ShowList();
if (1)
{
CMyClass e(1000);
cout << "The second end:" << endl;
CMyClass::ShowList();
}
delete d;
cout << "The third end:" << endl;
CMyClass::ShowList();
return 0;
}
类CMyClass中的vector类型的静态成员变量,目的是在创建对象时把他加入到容器中,在析构对象时把它从该容器中删除。现在debug,出现了overflow的错误,求教!

  • 写回答

2条回答 默认 最新

  • devmiao 2016-12-06 16:43
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog