![]() |
|
|
|||||||
| Регистрация | << Правила форума >> | FAQ | Пользователи | Календарь | Поиск | Сообщения за сегодня | Все разделы прочитаны |
![]() |
|
|
Опции темы | Поиск в этой теме | Опции просмотра |
|
#16
|
|||
|
|||
|
Цитата:
|
|
#17
|
|||
|
|||
|
Друзья попробывал создать класс, и нарисовать балку разного сечения 3 участка
что получилось естественно не компилирует, подскажите где ошибки Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
tuchastok=class
private
fdlina:real;
fploshad:real;
public
property dlina:real
read fdlina
write fdlina;
property ploshad:real
read fploshad
write fploshad;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
uchastok:array of tuchastok;
x:array of integer;
y:array of integer;
y_os:integer;
i, j:integer;
begin
x[0]:=100;
y[0]:=100;
for i:=1 to 3 do
begin
uchastok[i].dlina:=3*i;
uchastok[i].ploshad:=2*i;
end;
y_os:=y[0]+round((uchastok[1].ploshad*10)/2);
i:=1;
for j:=1 to 5 do
begin
if (j mod 2)>0
then
begin
x[j]:=round(uchastok[i].dlina*100);
x[j+1]:=round(uchastok[i].dlina*100);
i:=i+1;
end;
end;
i:=1;
for j:=1 to 5 do
begin
if j=1
then
begin
y[j]:=y_os+round((uchastok[i].ploshad*10)/2);
i:=i+1;
end
else
begin
if (j mod 2)=0
then
begin
y[j]:=y_os-round((uchastok[i].ploshad*10)/2);
y[j+1]:=y_os+round((uchastok[i].ploshad*10)/2);
i:=i+1;
end;
end;
end;
for i:=0 to 4 do
begin
form1.Canvas.Rectangle(x[i], y[i], x[i+1], y[i+1]);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
tuchastok.Create;
end;
end.что интересует правильно ли работаю с классом почему не могу уничтожить данные которые хранит объект free и destroy не понимает |
|
#18
|
||||
|
||||
|
Много ошибок. Конструктор Create нужно вызывать для каждого обьекта, с которым будешь работать, тоесть, если они у тебя в динамическом массиве, то типа так:
Код:
setlength(uchastok, 3); //размер массива становится 3, от 0 до 2
for i:=0 to 2 do
uchastok[i]:=TUchastok.Create; //для классов конструктор нужно вызывать так.
и вот здесь работаем с объектами.... не забывая, что в динамическом массиве индекс начинается с 0 и оканчивается {длина массива-1} |
|
#19
|
||||
|
||||
|
Все-таки нужно немного подучиться, а потом и что-то писать. Если нужна помощь, то нужно спрашивать, а не раз в месяц постить одно и то же, и снова молчать. Эта тема была, кажись, открыта в августе (учитывая предыдущую), а прогресса - 0.
![]() |
|
#20
|
|||
|
|||
|
Цитата:
Ну вот учусь, пишу, вы подсказываете |
|
#21
|
|||
|
|||
|
Доброе время суток
Исправил ошибки предыдущего кода все работает Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
tuchastok=class
private
fdlina:real;
fploshad:real;
public
property dlina:real
read fdlina
write fdlina;
property ploshad:real
read fploshad
write fploshad;
end;
var
Form1: TForm1;
uchastok:array of tuchastok;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i, j:integer;
x:array of integer;
y:array of integer;
y_os:integer;
begin
setlength(x, 7);
setlength(y, 7);
x[0]:=100;
y[0]:=100;
for i:=0 to 2 do
begin
uchastok[i].dlina:=3*i+2;
uchastok[i].ploshad:=2*i+2;
end;
y_os:=y[0]+round((uchastok[0].ploshad*10)/2);
i:=0;
for j:=1 to 5 do
begin
if (j mod 2)>0
then
begin
x[j]:=round(uchastok[i].dlina*100);
x[j+1]:=round(uchastok[i].dlina*100);
i:=i+1;
end;
end;
i:=0;
for j:=1 to 5 do
begin
if j=1
then
begin
y[j]:=y_os+round((uchastok[i].ploshad*10)/2);
i:=i+1;
end
else
begin
if (j mod 2)=0
then
begin
y[j]:=y_os-round((uchastok[i].ploshad*10)/2);
y[j+1]:=y_os+round((uchastok[i].ploshad*10)/2);
i:=i+1;
end;
end;
end;
for i:=0 to 4 do
begin
form1.Canvas.Rectangle(x[i], y[i], x[i+1], y[i+1]);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
setlength(uchastok, 3);
for i:=0 to 2 do
begin
uchastok[i]:=tuchastok.Create;
end;
end;
end.теперь данный алгоритм надо вынести в отдельный модуль написал, вот что получилось 1 модуль Код:
unit tuchastok;
interface
uses Controls, StdCtrls, SysUtils, Dialogs;
type
tuchastok=class
private
fdlina:real;
fploshad:real;
public
property dlina:real
read fdlina
write fdlina;
property ploshad:real
read fploshad
write fplshad;
end;
implementation
end.2 модуль Код:
unit balka;
interface
uses Controls, StdCtrls, SysUtils, Dialogs;
type
tbalka=class
private
fuchastok:array of tuchastok;
fshema:tcanvas;
procedure setshema(value:tcanvas);
public
property uchastok:array of tuchastok
read fuchastok
write fuchastok;
property shema:tcanvas
write setshema;
end;
implementation
uses tuchastok;
procedure tbalka.setshema(value:tcanvas);
var
x:array of integer;
y:array of integer;
y_os:integer;
i, j:integer;
begin
setlength(uchastok, 3);
setlength(x, 7);
setlength(y, 7);
x[0]:=100;
y[0]:=100;
y_os:=y[0]+round((uchastok[0].ploshad*10)/2);
i:=0;
for j:=1 to 5 do
begin
if (j mod 2)>0
then
begin
x[j]:=round(uchastok[i].dlina*100);
x[j+1]:=round(uchastok[i].dlina*100);
i:=i+1;
end;
end;
i:=0;
for j:=1 to 5 do
begin
if j=1
then
begin
y[j]:=y_os+round((uchastok[i].ploshad*10)/2);
i:=i+1;
end
else
begin
if (j mod 2)=0
then
begin
y[j]:=y_os-round((uchastok[i].ploshad*10)/2);
y[j+1]:=y_os+round((uchastok[i].ploshad*10)/2);
i:=i+1;
end;
end;
end;
for i:=0 to 4 do
begin
value.Rectangle(x[i], y[i], x[i+1], y[i+1]);
end;
shema:=value;
end.3 модуль Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses balka, tuchastok;
{$R *.dfm}
var
balka:tbalka;
procedure TForm1.FormCreate(Sender: TObject);
begin
balka:=tbalka.create;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
with balka do
begin
uchastok[0].dlina:=0.6;
uchastok[0].ploshad:=2*30;
uchastok[1].dlina:=0.4;
uchastok[1].ploshad:=30;
uchastok[2].dlina:=0.5;
uchastok[2].ploshad:=30;
end;
balka.setshema(form1.canvas);
end;
end.естественно не компилирует жду от вас помощи |
|
#22
|
||||
|
||||
|
Цитата:
|
|
#23
|
|||
|
|||
|
Цитата:
пишет что во 2 модуле нет идентификатора uchastok tcanvas [Error] balka.pas(10): Undeclared identifier: 'tuchastok' может uses надо модуль добавить какой-то? |
|
#24
|
||||
|
||||
|
Цитата:
|
|
#25
|
|||
|
|||
|
Цитата:
во 2 модуле есть uses tuchastok (модуль 2 строка 23) там описан этот класс |
|
#26
|
||||
|
||||
|
Код:
uses tuchastok; там, где описаны Код:
uses Controls, StdCtrls, SysUtils, Dialogs; |
|
#27
|
|||
|
|||
|
Цитата:
возникает другая ошибка (модуль 1 строка 8) повторно описано имя tuchastok, оно вроде только одно |
|
#28
|
||||
|
||||
|
Цитата:
|
|
#29
|
|||
|
|||
|
Цитата:
теперь другие ошибки [Error] BalkaUnit.pas(15): Identifier expected but 'ARRAY' found |
|
#30
|
||||
|
||||
|
Цитата:
Т.е. в твоём случае: Код:
type
tuchastok_array = array of tuchastok; // Объявляем новый тип
tbalka=class
private
fuchastok: tuchastok_array; // Теперь используем его здесь
fshema:tcanvas;
procedure setshema(value:tcanvas);
public
property uchastok: tuchastok_array // и здесь
read fuchastok
write fuchastok;
property shema:tcanvas
write setshema;
end;Последний раз редактировалось poli-smen, 16.11.2012 в 13:57. |