מערכים ומחלקות בC++ - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

מערכים ומחלקות בC++


SweeT_EviL

Recommended Posts

יותר מידי שגיאות. לא מצליח להתמודד עם השפה המיושנת =]]

הקוד ולאחריו שאלות:

mydate.h

#ifndef MYDATE
#define MYDATE

#include <stdio.h>
#include <string.h>

class MyDate
{
private:
static const int DAY_MIN_VALUE = 1;
static const int MONTH_MIN_VALUE = 1;
static const int YEAR_MIN_VALUE = 1901;
static const int DAY_MAX_VALUE = 30;
static const int MONTH_MAX_VALUE = 12;
static const int YEAR_MAX_VALUE = 2099;

int _day;
int _month;
int _year;
char *_comment;

/*
Checks if the date that the function gets is valid
Valid: day = 1,..,30 , except febuar where day = 1,..,28
month = 1,..,12
year = 1901,..,2099
*/
bool checkDate(int day, int month, int year);

public:
/*
The Ctor of the class put default values
*/
MyDate()
{
_day = DAY_MIN_VALUE;
_month = MONTH_MIN_VALUE;
_year = YEAR_MIN_VALUE;
_comment = NULL;
}

MyDate(const MyDate& date)
{
_day = date._day;
_month = date._month;
_year = date._year;

if(date._comment != NULL)
{
if(_comment!=NULL)
{
delete[]_comment;
}
_comment=new char[strlen(date._comment)+1];
strcpy(_comment,date._comment);
}
else
{
_comment = NULL;
}
}

/*
Sets the Date to 8.8.10
*/
void Init();

/*
Sets the date to the day, month and year that the function gets
*/
void Set(int day, int month, int year);

/*
Compare the two dates and return if the object date is earler then the date that he gets
*/
bool isBefore(MyDate date);

/*
Change the date's comment to be the comment that the function gets, max comment length - 20
*/
void changeComment(char *comment);

/*
Delay the date by delay, max value is 365. min value 1
*/
void Delay(int delay);

/*
Bring foward the date by foward, max value is 365. min value 1
*/
void bringFoward(int foward);

/*
Sets the day if its valid
Valid: 1,..,30 , except febuar where day = 1,..,28
*/
void setDay(int day);

/*
Sets the month if its valid
Valid: 1,..,12
*/
void setMonth(int month);

/*
Sets the year if its valid
Valid: 1901,..,2099
*/
void setYear(int year);

/*
Prints the date
*/
void Print();
};

#endif

calander.h

#include "MyDate.h"

#ifndef CALANDER
#define CALANDER

class Calander
{
private:
static const int CALANDER_SIZE = 30;
MyDate **_dates;
int _currentSize;
int _nextFreeSpace;

/*
Go over the array and finds the next free space
*/
int getNextFreeSpace();
public:
/*
The Ctor of the class, init all parameters
*/
Calander()
{
_dates = new MyDate[CALANDER_SIZE];
int i;
for(i = 0 ; i< CALANDER_SIZE ; ++i)
{
_dates[i] = NULL;
}

_currentSize = 0;
_nextFreeSpace = 0;
}

/*
Set the date parameter at the num place on the calander
*/
void setDate(int num, MyDate date);

/*
Gets a number from 1 to 30 and check if this place on the calander is free
*/
bool isFree(int num);

/*
Return the first place on the calander thats available, return 0 if it full
*/
int firstFree();

/*
Insert the date into the first available place on the calander
*/
void Insert(MyDate date);

/*
Return the earler date from the calander
*/
int Oldest();

/*
Return the number of cell that have dates
*/
int DatesNum();

/*
Delete all saved date on the calander
*/
void deleteAll();

/*
Delete specific date on num place
*/
void Delete(int num);

/*
Prints all the calander's dates
*/
void Print();
}


#endif

calander.cpp

#include "Calander.h"

/*
Set the date parameter at the num place on the calander
*/
void Calander::setDate(int num, MyDate date)
{
if(isFree(num))
{
_dates[num-1] = new MyDate(date);
++_currentSize;
if(num-1 == _nextFreeSpace)
{
_nextFreeSpace = getNextFreeSpace();
}
}
else
{
printf("The space on calander is not free\n");
}
}

/*
Gets a number from 1 to 30 and check if this place on the calander is free
*/
bool Calander::isFree(int num)
{
return _dates[num-1] == NULL;
}

/*
Return the first place on the calander thats available, return 0 if it full
*/
int Calander::firstFree()
{
if(_currentSize == CALANDER_SIZE)
{
return 0;
}
else
{
return _nextFreeSpace+1;
}
}

/*
Insert the date into the first available place on the calander
*/
void Calander::Insert(MyDate date)
{
if(_currentSize < CALANDER_SIZE)
{
_dates[_nextFreeSpace] = new MyDate(date);
++_currentSize;
_nextFreeSpace = getNextFreeSpace();
}
else
{
printf("Calander is full\n");
}
}

/*
Return the earler date from the calander
*/
int Calander::Oldest()
{
int min = -1;
if (_currentSize > 0)
{
int i;
for(i=0 ; i < CALANDER_SIZE ; ++i)
{
if(_dates[i] != NULL)
{
if(min == -1)
{
min = i;
}
else if(_dates[i].isBefore(_dates[min]))
{
min = i;
}
}
}
}
else
{
printf("No dates to compare - the calander is empty");
}

return min;
}

/*
Return the number of cell that have dates
*/
int Calander::datesNum()
{
return _currentSize;
}

/*
Delete all saved date on the calander
*/
void Calander::deleteAll()
{
int i;
for(i=0; i< CALANDER_SIZE ; ++i)
{
delete[]_dates[i];
_dates[i] = NULL;
}
}

/*
Delete specific date on num place
*/
void Calander::Delete(int num)
{
if(num >= 1 && num <= CALANDER_SIZE)
{
delete[]_dates[num-1];
}
else
{
printf("The specified location does not exist");
}
}

/*
Prints all the calander's dates
*/
void Calander::Print()
{
if(_currentSize > 0)
{
int i;
for(i=0; i< CALANDER_SIZE ; ++i)
{
if(_dates[i] != NULL)
{
_dates[i].print();
}
}
}
else
{
printf("Calander is empty");
}
}

/*
Go over the array and finds the next free space
*/
int Calander::getNextFreeSpace()
{
if(_currentSize == CALANDER_SIZE)
{
return -1;
}
else
{
int i;
for (i = _nextFreeSpace ; i < CALANDER_SIZE ; ++i )
{
if(_dates[i] == NULL)
{
return i;
}
}
}
return -1;
}

main.cpp

#include "Calander.h"

void main()
{
the content isnt relevant
}

השגיאות:

Error	2	error C2628: 'Calander' followed by 'void' is illegal (did you forget a ';'?)	d:\documents\visual studio 2008\projects\targil1\targil1\main.cpp	7	targil1
Error 3 error C3874: return type of 'main' should be 'int' instead of 'Calander' d:\documents\visual studio 2008\projects\targil1\targil1\main.cpp 8 targil1
Error 4 error C2440: '=' : cannot convert from 'MyDate *' to 'MyDate **' d:\documents\visual studio 2008\projects\targil1\targil1\calander.h 24 targil1

Error 7 error C2628: 'Calander' followed by 'void' is illegal (did you forget a ';'?) d:\documents\visual studio 2008\projects\targil1\targil1\calander.cpp 6 targil1
Error 8 error C2556: 'Calander Calander::setDate(int,MyDate)' : overloaded function differs only by return type from 'void Calander::setDate(int,MyDate)' d:\documents\visual studio 2008\projects\targil1\targil1\calander.cpp 7 targil1
Error 9 error C2371: 'Calander::setDate' : redefinition; different basic types d:\documents\visual studio 2008\projects\targil1\targil1\calander.cpp 7 targil1
Error 10 error C2228: left of '.isBefore' must have class/struct/union d:\documents\visual studio 2008\projects\targil1\targil1\calander.cpp 80 targil1
Error 11 error C2039: 'datesNum' : is not a member of 'Calander' d:\documents\visual studio 2008\projects\targil1\targil1\calander.cpp 98 targil1
Error 12 error C2065: '_currentSize' : undeclared identifier d:\documents\visual studio 2008\projects\targil1\targil1\calander.cpp 100 targil1

חוץ שגיאה 4 - WTF? במה אני טועה?

בקשר לשגיאה 4, במה אני טועה? וחוץ מזה איך הגישה ועבודה שלי עם המערך הדינאמי של מצביעים?

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

דרך אגב,

הדרישה היא ששדה המידע של CALANDER יכיל מערך דינאמי של מצביעים מטיפוס MYDATE ושגודל המערך הוא תמיד 30... ועל השיטה הזו עליתי. האם VECTOR גם עונה על זה? אני חשבתי שלא, דעתכם?

תודה על הכל!

קישור לתוכן
שתף באתרים אחרים

שגיאה 2: בסוף ההגדרה של מחלקה (אחרי הסוגר המסולסל) אתה צריך נקודה-פסיק. הקומפיילר אפילו אומר לך את זה.

שגיאה 4: הקריאה ל-new מחזירה מצביע לטיפוס, כלומר במקרה שלך זה *MyDate. על מנת לעשות מערך של מצביעים אתה צריך לעשות:

new MyDate*[size]

רוב השגיאות נובעות ממה שציינתי בשגיאה 2.

אתה יכול בינתיים להתעלם מהאזהרה על strcpy - קודם כל תדאג שהתכנית שלך תעבוד. אבל חוץ מזה, אם אתה עובד ב-++C, למה אתה עובד עם מחרוזות של C, ועם printf, במקום לעבוד עם string ועם cout?

קישור לתוכן
שתף באתרים אחרים

באמת עשה קצת סדר =]

שגיאה 10, אני אמרו לעשות CASTING בפירוש? הוא לא מכיר את MYDATE?

עריכה:

אני עובד עם CHAR כי זוהי ההגדרה...

בקשר לCOUT, פשוט לא עבדתי הרבה עם C++ וזוהי התוכנית הראשונה שאני כותב בC++ בשנתיים שלוש האחרונות..

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...