עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

סטטים בC++

Featured Replies

פורסם

היי,

מוחזרת לי השגיאה הבאה

Error	1	error C2065: 'maxGrade' : undeclared identifier

כאשר הקוד הוא:

.h

class Student{
private:
static int maxGrade;

public:
Student()
{}
static int getMaxGrade();
};

.cpp

#include "Student.h"

static int getMaxGrade()
{
return maxGrade;
}

ניסיתי להוסיף Student לפני הmaxGrade או לפני השם של הפונקציה אבל זה מחזיר לי שגיאה..

מה הבעיה?

תודה.

פורסם

יש לך שתי בעיות. קודם כל, כשאתה מצהיר (declare) על איבר סטטי במחלקה, עדיין צריך להגדיר (define) אותו איפשהו. בשביל זה אתה צריך להוסיף את השורה הבאה בקובץ ה-cpp שלך:

int Student::maxGrade;

הטעות השנייה שלך היא שכשאתה מממש מתודה של מחלקה (מה שבקובץ ה-cpp), אז אתה צריך לכלול את שם המחלקה בחתימה של המתודה - אחרת הקומפיילר לא יודע שהמתודה היא חלק מהמחלקה. כלומר, בקובץ ה-cpp אתה צריך לעשות:

int Student::getMaxGrade()
{
/// code goes here
}

בקוד שכתוב כרגע הגדרת פשוט פונקציה בשם getMaxGrade שאינה חלק משום מחלקה.

פורסם
  • מחבר

תודה.

אבל מה עם STATIC בשורת ההגדרה של הפונקציה? מספיק להגיד שהיא STATIC ב.H ?

פורסם

כן. למעשה, להגדיר משהו כ-static מחוץ למחלקה יש משמעות שונה לחלוטין.

ארכיון

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

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.