Форум программистов, компьютерный форум, киберфорум
С++ для начинающих
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
Рейтинг 4.80/5: Рейтинг темы: голосов - 5, средняя оценка - 4.80
4 / 4 / 4
Регистрация: 09.03.2014
Сообщений: 187
1

Ошибка при работе деструктора

12.02.2015, 21:28. Показов 996. Ответов 3
Метки нет (Все метки)

Author24 — интернет-сервис помощи студентам
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include<iostream>
#include<time.h>
#include<Windows.h>
 #include <stdio.h>
#include <conio.h>
#include <fstream>
#include <iomanip>
using namespace std;
class game
{
    char*packmen;
    char*kassper;
    char **map;
public:
    game();
    ~game();
    game(const game &B);
    void start_game();
    void continue_game();
    void save_game();
    int move_packmen();
    int move_kassper();
    int build_map();
    void show_map();
};
game::game()
{
    map=new char*[20];
    for(int i=0;i<20;i++)
        map[i]=new char[30];
    packmen=new char[1];
    kassper=new char[1];
}
game::game(const game &B)
{
    strcpy(packmen,B.packmen);
    strcpy(kassper,B.kassper);
    for(int i=0;i<20;i++)
          strcpy(map[i],B.map[i]);
}
game::~game()
{
    delete[]kassper;
    delete[]packmen;
    for(int i=0;i<20;i++)
    delete[]map[i];
}
int game::build_map()
{
    
    for(int i=0;i<20;i++)
    {
        if(i==0||i==19)
        {
        for(int j=0;j<30;j++)
            map[i][j]='=';
        }
        else
            for(int j=0;j<29;j++)
                map[i][j]=' ';
 
    }
    for(int j=0;j<20;j++)
            {
                map[j][29]='=';
            }
    int k=0,p=0;
    for(int i=1;i<30;i++)
        map[k=rand()%18+1][p=rand()%28+1]='|';
    packmen[0]='o';
    for(int i=1;i<19;i++)
    {
        for(int j=1;j<29;j++)
        {
    if(map[i][j]!='|')
    {
        map[i][j]=packmen[0]; 
        i=29;
    }
    else
          continue;
        }
        }
    kassper[0]='$';
    int calc=0;
    while(true)
    {
    for(int i=1;i<19;i++)
    {
        for(int j=0;j<29;j++)
        {
    if(map[i][j]==' ')
    {
        map[ i=rand()%17+1][j=rand()%27+1]=kassper[0]; 
        calc++;
        if(calc==3)
            return 0;
    }
    else
          continue;
        }
        }
    }
}
void game::show_map()
{
    Sleep(0);
    system("cls");
    for(int i=0;i<20;i<i++)
    {
        for(int j=0;j<30;j++)
            cout<<map[i][j];
    cout<<endl;
    }
}
int game::move_packmen()
{
    char*location=new char[1];
    while(true)
    {
        int move=move_kassper();
    if(move==119)
        for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='o')
                {
                    if(map[i-1][j]!='='&&map[i-1][j]!='|')
                    {
                    map[i][j]=' ';
                    map[i-1][j]='o';
                    show_map();
                    j=30;
                    i=29;
                    }
                    else continue;
                }
                else continue;
            if(move==115)
                for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='o')
                {
                    if(map[i+1][j]!='='&&map[i+1][j]!='|')
                    {
                    map[i][j]=' ';
                    map[i+1][j]='o';
                    show_map();
                    j=30;
                    i=29;
                    }
                    else continue;
                }
                else continue;
                if(move==97)
                for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='o')
                {
                    if(j-1>=0&&map[i][j-1]!='|')
                    {
                    map[i][j]=' ';
                    map[i][j-1]='o';
                    show_map();
                    j=30;
                    i=29;
                    }
                    else continue;
                }
                else continue;
                if(move==100)
                for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='o')
                {
                    if(map[i][j+1]!='='&&map[i][j+1]!='|')
                    {
                    map[i][j]=' ';
                    map[i][j+1]='o';
                    show_map();
                    j=30;
                    i=29;
                    }
                    else continue;
                }
                else continue;
                if(move==27)
                {
                    save_game();
                    return 0;
                }
 
    }
}
void game::save_game()
{
    ofstream outF("game.txt",ios::out);
    for(int i=0;i<20;i++)
    {
        for(int j=0;j<30;j++)
        {
    outF<<map[i][j];
        }
        outF<<endl;
    }
    outF.close();
}
int game::move_kassper()
{
    while(!kbhit())
    {
        Sleep(100);
        int move=rand()%4;
        if(move==0)
        for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='$')
                {
                    if(map[i-1][j]!='='&&map[i-1][j]!='|'&&map[i-1][j]!='$')
                    {
                    map[i][j]=' ';
                    map[i-1][j]='$';
                    show_map();
                    j=j++;
                    i=i++;
                    }
                    else break;
                }
            if(move==1)
                for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='$')
                {
                    if(map[i+1][j]!='='&&map[i+1][j]!='|'&&map[i+1][j]!='$')
                    {
                    map[i][j]=' ';
                    map[i+1][j]='$';
                    show_map();
                    j++;
                    i++;
                    }
                    else break;
                }
                if(move==2)
                for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='$')
                {
                    if(j-1>=0&&map[i][j-1]!='|'&&map[i][j-1]!='$')
                    {
                    map[i][j]=' ';
                    map[i][j-1]='$';
                    show_map();
                    j++;
                    i++;
                    }
                    else break;
                }
                if(move==3)
                for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='$')
                {
                    if(map[i][j+1]!='='&&map[i][j+1]!='|'&&map[i][j+1]!='$')
                    {
                    map[i][j]=' ';
                    map[i][j+1]='$';
                    show_map();
                    j++;
                    i++;
                    }
                    else break;
                }
    };
    int move=getch();
    return move;
}
void game::start_game()
{
    build_map();
    move_packmen();
    
}
void game::continue_game()
{
    ifstream inF("game.txt",ios::in| ios::_Nocreate);
        for(int i=0;i<1;i++)
    {
        for(int j=0;j<30;j++)
        {
    inF>>map[i][j];
        }
        //cout<<endl;
    }
    inF.close();
}
 
void main()
{
    //srand(time(NULL));
    game A;
    //A.start_game();
    A.build_map();
    A.show_map();
    //A.save_game();
    //A.continue_game();
    //A.show_map();
    system("pause");
}
я так понимаю конструктор копирования должен убрать ошибку но чего то не выходит(
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
12.02.2015, 21:28
Ответы с готовыми решениями:

Ошибка при работе деструктора объекта p1
тут простой класс очереди с конструктором копирования,деструктором,и функцией print,которая выводит...

Ошибка при вызове деструктора
Не могу дописать лабу... Надо создать 4 класса, описать поля и методы. Выкладываю исходники: Самый...

Ошибка при вызове деструктора
Добрый вечер! Почему при вызове деструктора вылетает ошибка после компиляции кода? #include...

Ошибка при вызове деструктора
Программа компилиться нормально,но после выполнения system(&quot;pause&quot;); выползает непонятное окно,и...

3
Модератор
Эксперт С++
13505 / 10756 / 6411
Регистрация: 18.12.2011
Сообщений: 28,711
12.02.2015, 21:51 2
В копиконструкторе тоже надо выделять память.
Но мне непонятно, зачем Вам вообще динамическая память.
Все размерности постоянные. Так и задайте их сразу.
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class game
{
    char packmen[1]; // Вы уверены, что массив должен быть из одного элемента. Для строки надо минимум 2.
// если все же один, то предлагаю поменять на  char packmen;
    char kassper[1];
    char map[30][20];
public:
    game();
    ~game();
    game(const game &B);
    void start_game();
    void continue_game();
    void save_game();
    int move_packmen();
    int move_kassper();
    int build_map();
    void show_map();
};
game::game()
{
   // ничего делать не надо
}
0
4 / 4 / 4
Регистрация: 09.03.2014
Сообщений: 187
12.02.2015, 22:51  [ТС] 3
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include<iostream>
#include<time.h>
#include<Windows.h>
 #include <stdio.h>
#include <conio.h>
#include <fstream>
#include <iomanip>
using namespace std;
class game
{
    char*packmen; 
    char*kassper;
    char **map;
public:
    game();
    ~game();
    game(const game &B);
    void start_game();
    void continue_game();
    void save_game();
    int move_packmen();
    int move_kassper();
    int build_map();
    void show_map();
};
game::game()
{
    map=new char*[20];
    for(int i=0;i<20;i++)
        map[i]=new char[30];
    packmen=new char[1];
    kassper=new char[1];
}
game::game(const game &B)
{
    packmen=new char[1];
    kassper=new char[1];
    map=new char*[20];
    for(int i=0;i<20;i++)
    map[i]=new char[30];
    strcpy(packmen,B.packmen);
    strcpy(kassper,B.kassper);
    for(int i=0;i<20;i++)
          strcpy(map[i],B.map[i]);
}
game::~game()
{
    delete[]kassper;
    delete[]packmen;
    for(int i=0;i<20;i++)
    delete[]map[i];
}
int game::build_map()
{
    
    for(int i=0;i<20;i++)
    {
        if(i==0||i==19)
        {
        for(int j=0;j<30;j++)
            map[i][j]='=';
        }
        else
            for(int j=0;j<29;j++)
                map[i][j]=' ';
 
    }
    for(int j=0;j<20;j++)
            {
                map[j][29]='=';
            }
    int k=0,p=0;
    for(int i=1;i<30;i++)
        map[k=rand()%18+1][p=rand()%28+1]='|';
    packmen[0]='o';
    for(int i=1;i<19;i++)
    {
        for(int j=1;j<29;j++)
        {
    if(map[i][j]!='|')
    {
        map[i][j]=packmen[0]; 
        i=29;
    }
    else
          continue;
        }
        }
    kassper[0]='$';
    int calc=0;
    while(true)
    {
    for(int i=1;i<19;i++)
    {
        for(int j=0;j<29;j++)
        {
    if(map[i][j]==' ')
    {
        map[ i=rand()%17+1][j=rand()%27+1]=kassper[0]; 
        calc++;
        if(calc==3)
            return 0;
    }
    else
          continue;
        }
        }
    }
}
void game::show_map()
{
    Sleep(0);
    system("cls");
    for(int i=0;i<20;i<i++)
    {
        for(int j=0;j<30;j++)
            cout<<map[i][j];
    cout<<endl;
    }
}
int game::move_packmen()
{
    char*location=new char[1];
    while(true)
    {
        int move=move_kassper();
    if(move==119)
        for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='o')
                {
                    if(map[i-1][j]!='='&&map[i-1][j]!='|')
                    {
                    map[i][j]=' ';
                    map[i-1][j]='o';
                    show_map();
                    j=30;
                    i=29;
                    }
                    else continue;
                }
                else continue;
            if(move==115)
                for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='o')
                {
                    if(map[i+1][j]!='='&&map[i+1][j]!='|')
                    {
                    map[i][j]=' ';
                    map[i+1][j]='o';
                    show_map();
                    j=30;
                    i=29;
                    }
                    else continue;
                }
                else continue;
                if(move==97)
                for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='o')
                {
                    if(j-1>=0&&map[i][j-1]!='|')
                    {
                    map[i][j]=' ';
                    map[i][j-1]='o';
                    show_map();
                    j=30;
                    i=29;
                    }
                    else continue;
                }
                else continue;
                if(move==100)
                for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='o')
                {
                    if(map[i][j+1]!='='&&map[i][j+1]!='|')
                    {
                    map[i][j]=' ';
                    map[i][j+1]='o';
                    show_map();
                    j=30;
                    i=29;
                    }
                    else continue;
                }
                else continue;
                if(move==27)
                {
                    save_game();
                    return 0;
                }
 
    }
}
void game::save_game()
{
    ofstream outF("game.txt",ios::out);
    for(int i=0;i<20;i++)
    {
        for(int j=0;j<30;j++)
        {
    outF<<map[i][j];
        }
        outF<<endl;
    }
    outF.close();
}
int game::move_kassper()
{
    while(!kbhit())
    {
        Sleep(100);
        int move=rand()%4;
        if(move==0)
        for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='$')
                {
                    if(map[i-1][j]!='='&&map[i-1][j]!='|'&&map[i-1][j]!='$')
                    {
                    map[i][j]=' ';
                    map[i-1][j]='$';
                    show_map();
                    j=j++;
                    i=i++;
                    }
                    else break;
                }
            if(move==1)
                for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='$')
                {
                    if(map[i+1][j]!='='&&map[i+1][j]!='|'&&map[i+1][j]!='$')
                    {
                    map[i][j]=' ';
                    map[i+1][j]='$';
                    show_map();
                    j++;
                    i++;
                    }
                    else break;
                }
                if(move==2)
                for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='$')
                {
                    if(j-1>=0&&map[i][j-1]!='|'&&map[i][j-1]!='$')
                    {
                    map[i][j]=' ';
                    map[i][j-1]='$';
                    show_map();
                    j++;
                    i++;
                    }
                    else break;
                }
                if(move==3)
                for(int i=0;i<20;i++)
            for(int j=0;j<30;j++)
                if(map[i][j]=='$')
                {
                    if(map[i][j+1]!='='&&map[i][j+1]!='|'&&map[i][j+1]!='$')
                    {
                    map[i][j]=' ';
                    map[i][j+1]='$';
                    show_map();
                    j++;
                    i++;
                    }
                    else break;
                }
    };
    int move=getch();
    return move;
}
void game::start_game()
{
    build_map();
    move_packmen();
    
}
void game::continue_game()
{
    ifstream inF("game.txt",ios::in| ios::_Nocreate);
        for(int i=0;i<1;i++)
    {
        for(int j=0;j<30;j++)
        {
    inF>>map[i][j];
        }
        //cout<<endl;
    }
    inF.close();
}
 
void main()
{
    //srand(time(NULL));
    game A;
    //A.start_game();
    A.build_map();
    A.show_map();
    //A.save_game();
    //A.continue_game();
    //A.show_map();
    system("pause");
}
все равно не пашет( даже после выделение памяти(
0
28 / 28 / 5
Регистрация: 23.04.2014
Сообщений: 130
13.02.2015, 00:30 4
в каком месте деструктор вызывается? при выходе из main()? Не вижу тут проблемы, связанной с копированием, т.к. создан только один объект
0
13.02.2015, 00:30
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
13.02.2015, 00:30
Помогаю со студенческими работами здесь

Ошибка при вызове деструктора
По окончании программы выдает ошибку #include &lt;string.h&gt; #include &lt;iostream&gt; using namespace...

Ошибка при вызове деструктора
Подскажите, что не так с этим деструктором? Просто выдает ошибку. Если его убираю то все идет норм....

Ошибка при вызове деструктора
Всем привет ! Есть класс class building { private: string *location_street;...

Ошибка при вызове деструктора
Здравствуйте, помогите разобраться с проблемой, она довольно распространенная и на форуме куча...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
4
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru