לוגו אתר Fresh          
 
 
  אפשרות תפריט  ראשי     אפשרות תפריט  צ'אט     אפשרות תפריט  מבזקים     אפשרות תפריט  צור קשר     חץ שמאלה ‎print ‎"Hello World!"; if‎ ‎not rules.‎know ‎then rules.‎read(); חץ ימינה  

לך אחורה   לובי הפורומים > מחשבים > תכנות ובניית אתרים
שמור לעצמך קישור לדף זה באתרי שמירת קישורים חברתיים
תגובה
 
כלי אשכול חפש באשכול זה



  #1  
ישן 15-06-2008, 02:51
  WolfsCaptain WolfsCaptain אינו מחובר  
 
חבר מתאריך: 30.01.07
הודעות: 261
C++ namespace

אוקי עברתי לאחרונה מג'אווה ל C++ סתם לראות מה אני מעדיף וכפי הנראה אני מעדיף C++ ^^

בכל מקרה, איך אני יכול לעשות שקובץ מקור שלם יהיה namespace או משהו כזה ואז להשתמש בו בקובץ מקור אחר ?
בנוסף לזה, אני רוצה לעשות קובץ מקור שיחזיק את כל המשתנים הגלובלים הקבועים (constant global) לנוחות, אבל כל פעם שאני מנסה איזו שהיא צורת כתיבה יש לי Circular error, שלפי הבנתי קורה כאשר קוד קורא לעצמו ?

ושאלה שנייה - כאשר מכריזים על מחלקה שיורשת ממחלקה אחרת, איך קוראים לבנאי ולאנטי בנאי שלה ?
ניסיתי הרבה ניסוחים ואף אחד מהם לא פועל

וסתם מתוך עניין, איך מכינים Sprites שזזים לפי הקוד ? (למשל הראש זז שהעכבר זז ודברים כאלה)
_____________________________________



נערך לאחרונה ע"י WolfsCaptain בתאריך 15-06-2008 בשעה 02:55.
תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
  #3  
ישן 15-06-2008, 03:37
  WolfsCaptain WolfsCaptain אינו מחובר  
 
חבר מתאריך: 30.01.07
הודעות: 261
בתגובה להודעה מספר 2 שנכתבה על ידי מאבטח שמתחילה ב "תשובות"

אני מעדיף שהמשתנים האלה יהיו גלובליים (מהירויות של דברים, גדלים, ושטויות כאלה).
בכל מקרה, הקוד די ארוך אבל הנה הוא (בעיקרון קוד של כדורים של רובה, או שחקן, בדו מימד)

קוד:
namespace Entity{ #include <CSpriteBase.h> #include <math.h> class Entity { public: Entity(float, float, float, float, float, char*, int); ~Entity(); float getx(); float gety(); float getangle(); float getspeed(); float getsize(); void setx(float); void sety(float); void setangle(float); void setspeed(float); void setsize(float); void setimage(char*); void moveentity(bool, bool); // false to ignore protected: CSpriteBase img; // entity image float x, y, angle, xV, yV, speed; int draw; int owner; float size; }; Entity::Entity(float e_x, float e_y, float e_angle, float e_speed, float e_size, char imgpath[], int e_owner) { x = e_x; y = e_y; angle = e_angle; speed = e_speed; size = e_size; xV = e_x + speed * cos(e_angle); yV = e_y + speed * sin(e_angle); img.init(imgpath); draw = 1; owner = e_owner; } Entity::~Entity() { draw = 0; } float Entity::getx() { return x; } float Entity::gety() { return y; } float Entity::getangle() { return angle; } float Entity::getspeed() { return speed; } float Entity::getsize() { return size; } void Entity::setx(float f) { x = f; } void Entity::sety(float f) { y = f; } void Entity::setangle(float f) { angle = f; } void Entity::setspeed(float f) { speed = f; xV = x + speed * cos(angle); yV = y + speed * sin(angle); } void Entity::setsize(float f) { size = f; } void Entity::setimage(char path[]) { img.init(path); } void Entity::moveentity(bool grav, bool wind) { x = x + xV; y = y + yV; if(grav) y = y - GRAVITY; if(wind) x = x + WIND_VELOCITY * cos(WIND_ANGLE); y = y + WIND_VELOCITY * sin(WIND_ANGLE); } class User: public Entity { public: User(float, float, float, float, float, char*, int); ~User(); void jump(); protected: int jumpnum; int usernum; }; User::User(float e_x, float e_y, float e_angle, float e_speed, int e_size, char imgpath[], int e_usernum) { x = e_x; y = e_y; angle = e_angle; speed = e_speed; size = e_size; xV = e_x + speed * cos(e_angle); yV = e_y + speed * sin(e_angle); img.init(imgpath); draw = 1; usernum = e_usernum; } User::~User() { draw = 0; } void User::jump() { yV = yV + JUMP_VELOCITY; } /* * *Entity functions * */ class Rect { public: Rect(float, float, float, float); ~Rect(); float getx(); float gety(); float getwidth(); float getheight(); void setx(float); void sety(float); void setwidth(float); void setheight(float); protected: float x, y, width, height; } void fireentity(int e_type, int e_owner) { if(e_type = 1) { Entity b = new Entity(usersArray[e_owner].getx(), usersArray[e_owner].gety(), facingAngle, WEAPON_ONE_SPEED, WEAPON_ONE_IMG, e_owner); } } int entitycollision(Entity one, Entity two) { float dist = sqrt(((one.getx() - two.getx())*(one.getx() - two.getx())) + ((one.gety() - two.gety())*(one.gety() - two.gety()))) if(dist >= one.getsize + two.getsize) return 1; return 0; } int rectcollision(Entity e, Rect r) { float ex = e.getx(); float rx = r.getx(); float ey = e.gety(); float ry = r.gety(); if(ex >= rx && ex <= rx + r.getwidth() && ey <= ry && ey >= ry + r.getheight()) return 1; return 0; } } // end namespace


בclass השני שמרחיב את Entity עשיתי בנאי ומפרק בשם של השני אבל זה לא פועל.
ובקשר לגלובאלים עוד פעם, בא לי לעשות משהו כזה
קוד:
namespace E_Const{ #define GRAVITY 9.8 #define GROUND_VELOCITY 5 #define GROUND_ACCEL 2 #define WIND_ANGLE 45 #define WIND_VELOCITY 2 #define AIR_VELOCITY = WIND_VELOCITY * 2 #define AIR_ACCEL = 4 #define JUMP_VELOCITY 15 #define SECOND_JUMP_VELOCITY 8 }


אני לא ממש בטוח אם אמורים להשתמש ככה ב define.
_____________________________________



נערך לאחרונה ע"י WolfsCaptain בתאריך 15-06-2008 בשעה 03:40.
תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
  #5  
ישן 15-06-2008, 15:24
  WolfsCaptain WolfsCaptain אינו מחובר  
 
חבר מתאריך: 30.01.07
הודעות: 261
בתגובה להודעה מספר 4 שנכתבה על ידי מאבטח שמתחילה ב "קצת מאוחר, אז כמה הערות מהירות:"

קוד:
namespace E_Const{ const float GRAVITY 9.8 const float GROUND_VELOCITY 5 const float GROUND_ACCEL 2 const float WIND_ANGLE 45 const float WIND_VELOCITY 2 const float AIR_VELOCITY = WIND_VELOCITY * 2 const float AIR_ACCEL = 4 const float JUMP_VELOCITY 15 const float SECOND_JUMP_VELOCITY 8 }


Circular E_Const <- E_Const.o dependency dropped.

ובקוד הגדול
קוד:
using namespace E_Const;


expected namespace-name before ';' token
`<type error>' is not a namespace

קוד:
class User: public Entity { public: User(float, float, float, float, float, char*, int); ~User(); void jump(); protected: int jumpnum; int usernum; }; User::User(float e_x, float e_y, float e_angle, float e_speed, int e_size, char imgpath[], int e_usernum) { x = e_x; y = e_y; angle = e_angle; speed = e_speed; size = e_size; xV = e_x + speed * cos(e_angle); yV = e_y + speed * sin(e_angle); img.init(imgpath); draw = 1; usernum = e_usernum; } User::~User() { draw = 0; }

prototype for `Entity::User::User(float, float, float, float, int, char*, int)' does not match any in class `Entity::User'
candidates are: Entity::User::User(const Entity::User&)

In constructor `Entity::User::User(float, float, float, float, int, char*, int)':
no matching function for call to `Entity::Entity::Entity()'
candidates are: Entity::Entity::Entity(const Entity::Entity&)
_____________________________________


תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
  #6  
ישן 16-06-2008, 01:30
  WolfsCaptain WolfsCaptain אינו מחובר  
 
חבר מתאריך: 30.01.07
הודעות: 261
בתגובה להודעה מספר 5 שנכתבה על ידי WolfsCaptain שמתחילה ב "[code]namespace..."

אוקי מחקתי את כל זה ופשוט עשיתי struct הרבה יותר פשוט כי לא צריך את כל השטויות האלה.
בכל מקרה, עכשיו שאני מנסה להשיג את ה x וה y של sprite עם השיטות שלו getx ו gety פתאום הוא טוען שאין לו שיטות כאלה (והשתמשתי בהם כבר לפני זה בהצלחה...)
הנה הקוד החדש
קוד:
#include <cstdlib> #include "CSprite.h" #include "CSpriteBase.h" #include <SDL/SDL_mixer.h> #include <SDL/SDL.h> #include <math.h> // constants const float GRAVITY = 9.8; const float GROUND_VELOCITY = 5; const float GROUND_ACCEL = 2; const float WIND_ANGLE = 45; const float WIND_VELOCITY = 2; const float AIR_VELOCITY = WIND_VELOCITY * 2; const float AIR_ACCEL = 4; const float JUMP_VELOCITY = 15; const float SECOND_JUMP_VELOCITY = 8; const float WEAPON_ONE_SPEED = 12; const float WEAPON_ONE_LIFE = 1; const int WEAPON_ONE_SIZE = 1; char* WEAPON_ONE_IMG = "path"; CSpriteBase weapon_one_base; const float WEAPON_TWO_SPEED = 4; const float WEAPON_TWO_LIFE = 2; const int WEAPON_TWO_SIZE = 2; char* WEAPON_TWO_IMG = "path"; CSpriteBase weapon_two_base; const float WEAPON_THREE_SPEED = 40; const float WEAPON_THREE_LIFE = 0.8; const int WEAPON_THREE_SIZE = 1; char* WEAPON_THREE_IMG = "path"; CSpriteBase weapon_three_base; struct Entity { int xV, yV; int bounce; int EType; CSprite ESprite; int owner; int size; }; struct Rect { int x, y, width, height; }; // globals SDL_Surface *screen; Entity entityArray[60]; int entityDraw[60]; /* * * * */ void entityMove(Entity e, bool grav, bool wind) { int x = e.ESprite.getx(); int y = e.ESprite.gety(); x = x + e.xV; y = y + e.yV; if(grav) y = y - GRAVITY; if(wind) x = x + WIND_VELOCITY * cos(WIND_ANGLE); y = y + WIND_VELOCITY * sin(WIND_ANGLE); e.ESprite.set(x, y); } int entitycollision(Entity one, Entity two) { float dx = one.ESprite.getx() - two.ESprite.getx(); float dy = one.ESprite.gety() - two.ESprite.gety(); float dist = sqrt(dx * dx + dy * dy); if(dist >= one.size + two.size) return 1; return 0; } int rectcollision(Entity e, Rect r) { float ex = e.ESprite.getx(); float rx = r.x; float ey = e.ESprite.gety(); float ry = r.y; if(ex >= rx && ex <= rx + r.width && ey <= ry && ey >= ry + r.height) return 1; return 0; } /* * * * */ int main(int argc, char *argv[]) { if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0) { printf("Unable to init SDL: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|S DL_FULLSCREEN); if ( screen == NULL ) { printf("Unable to set 640x480 video: %s\n", SDL_GetError()); exit(1); } weapon_one_base.init(WEAPON_ONE_IMG); weapon_two_base.init(WEAPON_TWO_IMG); weapon_three_base.init(WEAPON_THREE_IMG); for(int i=0;i<60;i++) { if(i < 20) entityArray[i].ESprite.init(&weapon_one_base,screen); if(i < 40 && i > 20) entityArray[i].ESprite.init(&weapon_two_base,screen); if(i < 60 && i > 40) entityArray[i].ESprite.init(&weapon_three_base,screen); } }
_____________________________________


תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
תגובה

כלי אשכול חפש באשכול זה
חפש באשכול זה:

חיפוש מתקדם
מצבי תצוגה דרג אשכול זה
דרג אשכול זה:

מזער את תיבת המידע אפשרויות משלוח הודעות
אתה לא יכול לפתוח אשכולות חדשים
אתה לא יכול להגיב לאשכולות
אתה לא יכול לצרף קבצים
אתה לא יכול לערוך את ההודעות שלך

קוד vB פעיל
קוד [IMG] פעיל
קוד HTML כבוי
מעבר לפורום



כל הזמנים המוצגים בדף זה הם לפי איזור זמן GMT +2. השעה כעת היא 20:44

הדף נוצר ב 0.05 שניות עם 12 שאילתות

הפורום מבוסס על vBulletin, גירסא 3.0.6
כל הזכויות לתוכנת הפורומים שמורות © 2026 - 2000 לחברת Jelsoft Enterprises.
כל הזכויות שמורות ל Fresh.co.il ©

צור קשר | תקנון האתר