PDA

View Full Version : کمک برای رفع ارورهای این کد



fshb_ 1370
یک شنبه 27 آذر 1390, 10:07 صبح
سلام
کد این برنامه وقتی برنامه run میشه ارور میده، مشکلش چیه؟ warning هایی که کد در هنگام compile میده،چگونه باید رفعش کنم؟

اگر در فایل Cow.cpp به جای strncpy(hobby,ho) ، عبارت *hobby=*ho بذارم، چه فرقی میکنه و چرا ارور میده؟

اگر در تابع operator=() به جای if(this==&c) از if(this== c) استفاده کنم، چرا error میده؟

ممنون از همه
Cow.h

#ifndef COW_H_
#define COW_H_
class Cow
{
private:
char name[20];
char *hobby;
double weight;
public:
Cow();
Cow(const char *nm,const char *ho,double wt);
Cow(const Cow & c);
~Cow();
Cow & operator=(const Cow & c);
void ShowCow()const;
};
#endif


Cow.cpp

#include<iostream>
#include<cstring>
using namespace std;
#include "COW.h"
Cow::Cow()
{
strcpy(name,"mag");
hobby=new char[20];
hobby="mage";
weight=10;
}
Cow::Cow(const char *nm, const char *ho, double wt)
{
strcpy(name,nm);
hobby=new char[strlen(ho)+1];
*hobby=*ho;
//strcpy(hobby,ho);
weight=wt;
}
Cow::Cow(const Cow &c)
{
strcpy(name,c.name);
hobby=new char[strlen(c.hobby)+1];
//hobby=c.hobby;
strcpy(hobby,c.hobby);
weight=c.weight;
}
Cow::~Cow()
{
delete [] hobby;
}
Cow & Cow::operator =(const Cow &c)
{
//
if(this==&c)
return *this;
delete [] hobby;
strcpy(name,c.name);
hobby=new char[strlen(c.hobby)+1];
//hobby=c.hobby;
strcpy(hobby,c.hobby);
weight=c.weight;
return *this;
}
void Cow::ShowCow() const
{
cout<<"name is: "<<name<<endl;
cout<<"hobby is: "<<hobby<<endl;
cout<<"weight is: "<<weight<<endl;
}


CowMian.cpp
#include<iostream>
using namespace std;
#include "Cow.h"
int main()
{
//char *cp="test";
Cow c1;
Cow c2("for","test",10);
Cow c3=c2;
c2=c1;
c1.ShowCow();
c2.ShowCow();
c3.ShowCow();
return 0;
}

shahmohammadi
یک شنبه 27 آذر 1390, 18:18 عصر
سلام.
برای هر فایل موارد درست رو مینویسم.

Cow.h:
Cow(const char *nm,const char *ho,double wt);

constchar اشتباست.

Cow.cpp:
using namespace std;
hobby=new char[strlen(c.hobby)+1];

حتما متوجه این قسمت هم شدید که new char ها رو چسبیده نوشتید.

CowMian.cpp:
#include "Cow.c"


موفق باشید.

fshb_ 1370
دوشنبه 28 آذر 1390, 08:57 صبح
این اشکالات املایی در copy کردن کد به وجود آمده در فایل اصلی این ایرادات وجود نداره. مشکل جای دیگه است.
مرسی از پاسختون

Celestial Girl
دوشنبه 28 آذر 1390, 13:03 عصر
این یه برنامه گرافیکی به زبان سی برای حرکت توپ هست داخل محدوده دایره
به نظرتون چرا ارور میده ؟


#include <dos.h>
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include <math.h>


int x=320,y=240;
int dx=1,dy=1;

void showball(){
setcolor(12);
circle(x,y,10);
setfillstyle(1,12);
floodfill(x,y,12);
}

void hideball(){
setcolor(0);
circle(x,y,10);
setfillstyle(1,0);
floodfill(x,y,0);
}

void moveball(){
x+=dx;
y+=dy;
if(x>=640 || x<=0)
dx=-dx;
if(y>=420 || y<=0)
dy=-dy;
}

//sharte bargashte toop
void check(){
float d;
int m;
m=(((320-x)*(320-x))-((240-y)*(240-y)));//fasele toopaz markaze dayere
d=sqr(2)m;
if (d>200){
x=-x;
y=-y;
}
}

void main(void){
int gd=DETECT,gm;
initgraph(&gd,&gm,"\\bc\\bgi");
for(int i=0;i<5;i++){
setcolor(5);
circle(320,240,200+i);
circle(320,241,200+i);

}
do{
showball();
delay(10);
hideball();
moveball();
// hideball();
check();
moveball();
hideball();


}while(!kbhit());

getch();
closegraph();
}

shahmohammadi
دوشنبه 28 آذر 1390, 15:59 عصر
این اشکالات املایی در copy کردن کد به وجود آمده در فایل اصلی این ایرادات وجود نداره. مشکل جای دیگه است.
مرسی از پاسختون برنامه تون با اصلاحاتی که من کردم کامل کار می کنه.


این یه برنامه گرافیکی به زبان سی برای حرکت توپ هست داخل محدوده دایره
به نظرتون چرا ارور میده ؟ با اصلاح زیر دیگه ارور نمی ده. (توی تربو سی داس کامپایل کنید)
البته اگه قصدتون اینه که توپ داخل دایره حرکت کنه الگوریتم درست نیست.
#include <dos.h>
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include <math.h>


int x=320,y=240;
int dx=1,dy=1;

void showball(){
setcolor(12);
circle(x,y,10);
setfillstyle(1,12);
floodfill(x,y,12);
}

void hideball(){
setcolor(0);
circle(x,y,10);
setfillstyle(1,0);
floodfill(x,y,0);
}

void moveball(){
x+=dx;
y+=dy;
if(x>=640 || x<=0)
dx=-dx;
if(y>=420 || y<=0)
dy=-dy;
}

//sharte bargashte toop
void check(){
float d;
int m;
m=(((320-x)*(320-x))-((240-y)*(240-y)));//fasele toopaz markaze dayere
d=sqrt(m);
if (d>200){
x=-x;
y=-y;
}
}

void main(void){
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tctemp");
for(int i=0;i<5;i++){
setcolor(5);
circle(320,240,200+i);
circle(320,241,200+i);

}
do{
showball();
delay(10);
hideball();
moveball();
// hideball();
check();
moveball();
hideball();


}while(!kbhit());

getch();
closegraph();
}

Celestial Girl
دوشنبه 28 آذر 1390, 20:26 عصر
ممنون از لطفتون آره میخوام تو دایره حرکت کنه ... اگه اشتباهه کجاش رو باید درست کنم ؟

منظورتون اینه که با داس باکس باز کنم ؟ من با محیط بورلند سی کار میکنم برای کارهای گرافیکی و داس باکس .
اینو تو ایم محیط اجرا کردم مشکلش حل نشده بود و از دایره بزرگ میومد بیرون و یه چیزهایی تایپ میشد تو محیط بازی بنام : sqrt domain erroor و همینوجوری تایپ میشد تا پایین میومد .....

shahmohammadi
چهارشنبه 30 آذر 1390, 11:17 صبح
#include <dos.h>
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include <math.h>


int x=320,y=240;
int dx=1,dy=2,sx,sy;

void showball(){
setcolor(12);
circle(x,y,10);
setfillstyle(1,12);
floodfill(x,y,12);
}

void hideball(){
setcolor(0);
circle(x,y,10);
setfillstyle(1,0);
floodfill(x,y,0);
}

void moveball(){
x+=dx;
y+=dy;

if(x>=640 || x<=0)
dx=-dx;
if(y>=420 || y<=0)
dy=-dy;
}

//sharte bargashte toop
void check(){
float d;
float m;
m=(((320-x)*(320-x))+((240-y)*(240-y)));//fasele toopaz markaze dayere
// d=sqrt((float)m);
if (m<200*200){
dx=-dx;
dy=-dy;
}
}

void main(void){
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tctemp");
for(int i=0;i<5;i++){
setcolor(5);
circle(320,240,200+i);
circle(320,241,200+i);

}
do{
showball();
delay(10);
hideball();
moveball();
// hideball();
check();
moveball();
hideball();


}while(!kbhit());

getch();
closegraph();
}

Celestial Girl
پنج شنبه 01 دی 1390, 01:10 صبح
ممنون از لطفتون تقریبا مشکلم حل شد ولی برنامه ای که من نوشتم سورس کاملش به این شکله :
یعنی بازی تنیس ! شرط باخت که اگه به دیواره دایره بزرگ خورد باخت و شرط برگشت هم میخوام برخورد به کمان تنیس باشه .... باید چه کاری کنم ؟
مممنون



#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <graphics.h>
#include <dos.h>


int x=320,y=240;

int dx=1,dy=2,sx,sy;


void showball(){
setcolor(12);
circle(x,y,10);
setfillstyle(1,12);
floodfill(x,y,12);
}

void hideball(){
setcolor(0);
circle(x,y,10);
setfillstyle(1,0);
floodfill(x,y,0);
}


void moveball(){
x+=dx;
y+=dy;

if(x>=640 || x<=0)
dx=-dx;
if(y>=420 || y<=0)
dy=-dy;
}

/*void initball(){
x=random(520);
y=random(440);
dx=random(2)+1;
dy=random(2)+1;
}*/



//sharte bargashte toop

void check(){
float d;
float m;
m=(((320-x)*(320-x))+((240-y)*(240-y)));//fasele toopaz markaze dayere
// d=sqrt((float)m);
if (m<200*200){
dx=-dx;
dy=-dy;

}

}


void mouse_init(void){
struct REGPACK reg;
reg.r_ax = 0x0;
intr(0x33, &reg);
}
void mouse_show(void){
struct REGPACK reg;
reg.r_ax = 0x1;
intr(0x33, &reg);
}

void mouse_hide(void){
struct REGPACK reg;
reg.r_ax = 0x2;
intr(0x33, &reg);
}

int mouse_lclick(void){
struct REGPACK reg;
reg.r_ax = 0x3;
intr(0x33, &reg);
return(reg.r_bx & 1);
}
int mouse_rclick(void){
struct REGPACK reg;
reg.r_ax = 0x3;
intr(0x33, &reg);
return(reg.r_bx & 2);
}
int mouse_x(void){
struct REGPACK reg;
reg.r_ax = 0x3;
intr(0x33, &reg);
return(reg.r_cx);
}

int mouse_y(void){
struct REGPACK reg;
reg.r_ax = 0x3;
intr(0x33, &reg);
return(reg.r_dx);
}

int mouse_move(void){
struct REGPACK reg;
reg.r_ax = 0xb;
intr(0x33, &reg);
return(reg.r_dx || reg.r_cx);
}

void show_rocket(int a,int color){
setcolor(color);
for(int i=0;i<5;i++){
arc(320,240,a,a+30,190+i);
arc(320,241,a,a+30,190+i);
}

}

void main(void){
int gd=DETECT,gm;
initgraph(&gd,&gm,"\\bc\\bgi");
mouse_init();
for(int i=0;i<5;i++){
setcolor(3);
circle(320,240,200+i);
circle(320,241,200+i);
}
mouse_hide();
float a=0;
show_rocket(a,13);

while(!kbhit()){
showball();
delay(10);
hideball();
moveball();
// hideball();
check();
moveball();
hideball();

if(mouse_move()){
show_rocket(a,0);
a=atan((240-mouse_y())/(mouse_x()-320+0.01));
a=a*180.0/3.14-15;
if(mouse_x()<320)
a+=180;
show_rocket(a,13);
delay(20);
}
if(x==mouse_x() & y==mouse_y()){
x=-x;
y=-y;
}
}
getch();
closegraph();
}

fshb_ 1370
جمعه 02 دی 1390, 09:36 صبح
فک میکنم موضوع تاپیک من کاملا با سوالی که شما این جا پرسیدید بی ربط باشه. لطف کیند از این به بعد سوالاتون در تاپیک جداگانه ای مطرح کنید.

Celestial Girl
جمعه 02 دی 1390, 13:28 عصر
فک میکنم موضوع تاپیک من کاملا با سوالی که شما این جا پرسیدید بی ربط باشه. لطف کیند از این به بعد سوالاتون در تاپیک جداگانه ای مطرح کنید.کدهایی که من نوشته بودم ارور داشتن برای همین نخواستم تاپیک اضافی درست کنم و مشکلم رو اینجا مطرح کردم
به نظرم بهتره یه تاپیک کلی برای برنامه نویسی به زبان سی ایجاد شه از طرف مدیریت انجمن و تمام مشکلات مربوط به برنامه نویسی در اونجا بیان شه تا از زدن تاپیک های اضافی جلوگیری شه و تمام بحث ها یکجا متمرکز بمونه

shahmohammadi
جمعه 02 دی 1390, 19:54 عصر
یه راهنمایی کلی در این برنامه رو می نویسم:

موقعی که شرط برخورد اعمال شد
1. x,y ابتدا و انتهای کمان راکت رو بدست بیارید.
2. از روی دو نقطه جدید یه چهارضلعی فرضی درست کنید
3. بررسی کنید ببینید که آیا نقطه مرکز توپ (x,y توپ) در داخل این مربع هست یانه.
79577
http://barnamenevis.org/images/misc/pencil.png