我有这个小哑的程序,我必须为我的 class 做些什么 . 它工作正常,它输出正确,但最后Visual Studio说该程序触发了一个断点,在我按下继续后它显示了这个:

ERROR1

在按下“Ismét”(再次表示)之后,它再次向我显示Visual Studio断点窗口,并且在执行此操作5次后(每次尝试显示不同的错误)程序关闭 .

ERRORS

我发现 free 命令试图清除一些垃圾,因为没有指定变量,但正如你所看到的,在我的情况下,我总是指定我的 tomb 指针 .

class1.cpp:

#include <iostream>
#include <stdlib.h>
using namespace std;

class kotprog {

private:

int a;
int b;


protected:
int *tomb = (int*)malloc(sizeof(int) * 3);
public:
kotprog(int c, int d) {
    a = c;
    b = d;
    tomb[0] = 0;
    tomb[1] = 0;
}
kotprog() {
    tomb[0] = 0;
    tomb[1] = 0;
}



void setA(int a) {
    this->a = a;
}
void setB(int b) {
    this->b = b;
}
int metodus1() {
    return a + b;
}
int metodus2() {
    return a - b;
}
int metodus3() {
    return a*b;
}
double metodus4() {
    return (double)a / (double)b;
}
void metodus5() {
    cout << "Egy teljesen buta kötelező program vagyok, mert teremtőmnek nem volt kedve többet foglalkozni velem. :(" << "\n";
    tomb[0] = this->a;
    tomb[1] = this->b;
    cout << "A tömb első eleme: " << tomb[0] << ". A tömb 2. eleme: " << tomb[1] << "." << "\n";
}

kotprog operator+(kotprog& b) {
    return kotprog(this->a + b.a, this->b + b.b);
}

friend ostream& operator<<(ostream&, kotprog&a) {

    return cout << "Az 1. paraméter: " << a.a << "\n" << "A második paraméter: " << a.b;

}
~kotprog() {
    free(tomb);

}
};

main.cpp:

#include<iostream>
#include"class1.cpp"
using namespace std;


int main() {


kotprog* teszt = new kotprog(2,3);
kotprog teszt2(2,2);
kotprog teszt3(3, 3);
kotprog teszt4;



teszt->setA(12);
teszt->setB(12);

cout << "1. metódus eredménye: " << teszt->metodus1() << "\n";
cout << "2. metódus eredménye: " << teszt->metodus2() << "\n";
cout << "3. metódus eredménye: " << teszt->metodus3() << "\n";
cout << "4. metódus eredménye: " << teszt->metodus4() << "\n";
teszt->metodus5();

teszt4 = teszt2 + teszt3;
cout << "A teszt4 eredménye: " << "\n" << teszt4;
delete teszt;
}