【问题标题】:I got a memory error and i dont know whats cauising it in c++ [closed]我有一个内存错误,我不知道是什么导致它在 C++ [关闭]
【发布时间】:2021-08-07 19:46:19
【问题描述】:

您好,这段代码是大代码的一部分,它会导致很多问题,我不知道为什么。我尝试使用 structer 和向量,它得到一个关于向量超出范围的错误,然后我切换到类,但仍然得到同样的错误,我现在编译只是有问题,它看起来像一个内存错误。我该如何解决这个问题?

#include <time.h>
#include <locale.h>
#include <vector>
#include <exception>
using namespace std;

class işlem {
    string işlemdetay; int işlemtutar; time_t işlemtarih; int hesapno;

public:
    vector <işlem> işlemler;
    işlem() { işlemdetay = " "; işlemtutar = 0; işlemtarih = time_t(0); hesapno = 0; };
    işlem(string İşlemdetay, int İşlemtutar, int Hesapno) {
        işlemdetay = İşlemdetay; işlemtutar = İşlemtutar; hesapno = Hesapno;

        işlem newişlem(işlemdetay, işlemtutar, hesapno);
        işlemler.push_back(newişlem);
    }
    void listele(int hesapid) {
        int size = işlemler.size();
        for (; size >= 0; size--)
        {
            if (işlemler[size].hesapno == hesapno)
            {
                //cout << "İşlem Tarihi: " << işlemler[size].işlemtarih << endl;
                cout << "İşlem Tutar: " << işlemler[size].işlemtutar << endl;
                cout << "İşlem Detayı: " << işlemler[size].işlemdetay << endl;
            }
        }
    }
};

int main()
{
    try
    {
        işlem newişlem("Hesap oluşturuldu.", 400, 1);
    }
    catch (const std::exception e)
    {
        cout << e.what();
    }
    
}```

【问题讨论】:

  • 到底是什么错误?请edit您的问题并添加逐字错误消息。

标签: c++ class memory vector


【解决方案1】:

işlemler[işlemler.size()] 超出范围。 size的初始值应该是işlemler.size() - 1,而不是işlemler.size()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多