Bonjour,

J'ai récupéré une classe, dans un livre, et le programme qui s'en sert.

L'affichage est presque celui attendu, sauf qu'il y a un "double bip" à la compilation (carte mère, ram ?), et un message d'erreur, en plus, qui "stoppe" l'exécution.

La question est très vague, puisque je suis incapable de comprendre d'où viennent ces 2 problèmes.

Au Debug il n'y a pas d'"erreur" (3 warnings pour conversin from int 64 to int, avec possible perte de data, mais rien de bien méchant).

Je pense que le bug (pas les bips) vient de la fonction
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
Screen& Screen::copy(Screen &s){
	delete screen;
	height = s.height;
	width = s.width;
 
	screen = cursor = new char[ height * width + 1];
	assert (screen != 0);
	strcpy_s (screen, sizeof(s.screen), s.screen);
	return *this;
}
à cause de la fonction strcpy_s() que j'ai utilisé pour remplacer strcpy() (qui provoque une erreur), tout en rajoutant dans le .h :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
#define _ _STDC_WANT_SECURE_LIB_ _ 1
Mais je l'ai utilisé un peu au hasard pour ne plus plus avoir d'erreur.

Par contre, le double bip, je ne sais pas d'où il vient (c'est grave ??)

Voici les deux fichiers :
-Screen.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include "stdafx.h"
#define _ _STDC_WANT_SECURE_LIB_ _ 1
#include <iostream>
#include <stdlib.h>
#include <string>
#include <assert.h>
using namespace std;
 
 
class Screen {
public :
	Screen(int = 8, int = 40, char = '#');
	inline int remainingSpace();
	inline int row();
	int isEqual(Screen&);
	inline Screen& down();
	inline Screen& forward();
	inline Screen& move(int, int);
	Screen& home(){move (1, 1); return *this;}
	Screen& checkRange(int, int);
	Screen& copy(Screen&);
	Screen& display();
	Screen& lineX(int, int, int, char);
	Screen& reSize(int, int, char);
	Screen& set(char);
	Screen& set(char*);
private :
	short height, width;
	char *cursor, *screen;
};
 
 
Screen::Screen(int high, int wid, char bkground){
	int sz = high * wid;
	height = high;
	width = wid;
	cursor = screen = new char[sz + 1];
	assert (cursor !=0);
 
	char *ptr = screen;
	char *endptr = screen + sz;
	while (ptr != endptr) *ptr++ = bkground;
	*ptr = '\0';
}
inline int Screen::remainingSpace(){
	int sz = width*height;
	return (screen + sz - cursor - 1);
}
inline int Screen::row(){
	int pos = cursor - screen + 1;
	return (pos + width - 1) / width;
}
int Screen::isEqual(Screen &s){
	if(width != s.width || height != s.height)
		return 0;
	char *p = screen;
	char *q = s.screen;
	if (p == q) return 1;
	while (*p && *p++ == *q++);
	if (*p)
		return 0;
	return 1;
}
inline Screen& Screen::down(){
	const char BELL = '\007';
	if (row() == height)
		std::cout.put (BELL);
	else
		cursor += width;
	return *this;
}
inline Screen& Screen::forward(){
	++cursor;
	if (*cursor == '\0')
		home ();
	return *this;
}
inline Screen& Screen::move(int r, int c){
	checkRange (r, c);
	int row = (r-1)*width;
	cursor = screen + row + c - 1;
	return *this;
}
Screen& Screen::checkRange (int row, int col){
	if (row < 1 || row > height ||col < 1 || col > width){
		std::cerr<< "Screen coordinates ( "<<row<<", "<<col<<" ) out of bounds."<<endl;
		exit (-1);
	}
	return *this;
}
Screen& Screen::copy(Screen &s){//FONCTION COPY
	delete screen;
	height = s.height;
	width = s.width;
 
	screen = cursor = new char[ height * width + 1];
	assert (screen != 0);
	strcpy_s (screen, sizeof(s.screen), s.screen);
	return *this;
}
Screen& Screen::display(){
	for(int ix = 0; ix <height; ix++){
		std::cout<<endl;
		int offset = width * ix;
		for (int j = 0; j<width; j++){
			char*p = screen + offset + j;
			std::cout.put(*p);
		}
	}
	return *this;
}
Screen& Screen::lineX(int row, int col, int len, char ch){
	move (row, col);
	for(int ix = 0; ix<len; ix++)
		set(ch).forward();
	return *this;
}
Screen& Screen::reSize(int h, int w, char bkground){
	Screen *ps = new Screen (h, w, bkground);
	assert (ps !=0);
	char *pNew = ps->screen;
	if (screen){
		char *pOld = screen;
		while (*pOld && *pNew)
			*pNew++ = *pOld++;
		delete screen;
	}
	*this = *ps;
	return *this;
}
Screen& Screen::set(char*s){
	int space = remainingSpace();
	int len = strlen(s);
	if(space<len){
		std::cerr<<"Screen : warning : truncation : "<<endl<<"space : "<<space<<"string lenght : "<<len<<endl;
		len = space;
	}
	for(int ix = 0; ix<len; ++ix)
		*cursor++ = *s++;
	return *this;
}
Screen& Screen::set(char ch){
	if(ch=='\0')
		cerr<<"Screen : warning : "<<endl<<"null character (ignored)."<<endl;
	else *cursor = ch;
	return *this;
}
Screen& lineY(Screen& s, int row, int col, int len, char ch){
	s.move(row,col);
	for(int ix = 0; ix<len; ix++)
		s.set(ch).down();
	return s;
}
-ClassScreen_3.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include "stdafx.h"
#include "Screen.h"
#include <iostream>
using namespace std;
 
 
int main(){
	int tmp = 0;
 
	Screen x(3,3);
	Screen y(3,3);
	std::cout<<"isEqual (x,y): (>1<)"
		<<x.isEqual(y)<<endl;
	y.reSize(6,6,'#');
	std::cout<<"isEqual (x,y): (>0<)"
		<<x.isEqual(y)<<endl;
	lineY(y,1,1,6,'*');
	lineY(y,1,6,6,'*');
	y.lineX(1,2,4,'*').lineX(6,2,4,'*').move(3,3);
	y.set("hi").lineX(4,3,2,'^').display();
 
	x.reSize(6,6,'#');
	std::cout<<endl<<endl<<"isEqual (x,y): (>0<)"
		<<x.isEqual(y)<<endl;
	x.copy(y); // BUGGE ICI
	std::cout<<"isEqual (x,y): (>1<)"
		<<x.isEqual(y)<<endl;
 
 
	std::cin>>tmp;
	return 0;
}
Et enfin, les captures écran du problème...


_________________________________________________________________________________________
Et la seconde avec la nature du conflit..
Apparemment, il n'y a plus de place ?
_________________________________________________________________________________________



_________________________________________________________________________________________
Bref, je comprends pas.. Si une âme généreuse voulait bien m'éclairer sur la nature du problème................