עבור לתוכן
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 שגיאת invalid null pointer

Featured Replies

פורסם

מה זה אומר invalid null pointer ?

מצורף חלק מהקוד.

אם, לדוגמא,בקובץ main אני רושמת שורה כמו:

;(Student *t = new Student("g","fn",4

השגיאה מופיעה בשורה של אתחול מערך course.

h file:

[/code]

 # include <string>
# include <iostream>
# define Non Course()
using namespace std;

///////////////// 2 /////////////////
struct Course{
int Number;
string Name;
/////////////////// בנאי ///////////////////
Course(int number = NULL , string name = "NULL" ){
Number = number;
Name = name;
}
///////////////// בנאי מעתיק ///////////////
Course (const Course &c){
Number = c.Number;
Name = c.Name;
}
///////////// הדפסת פרטי הקורס ////////////////
void printCourse () { cout<<Number<<" : "<<Name<<endl; }

///// השיטה מקבלת קורס ומשווה בין הקורסים ///////
bool equals(Course c){
return (this->Name == c.Name && this->Number == c.Number);

}
};


class Student
{
friend struct Course;
private:
string Name;
string Address;
long ID;
/////////////// 3 //////////////

public:
Course *Courses;


Student(void);
Student (string name, string address, long id);
~Student(void);
};

cpp file:

#include "StdAfx.h"
#include "Student.h"
# define Non Course(NULL,NULL)


Student::Student(void){

}
Student::Student (string name, string address, long id){
Name = name;
Address = address;
ID = id;
Courses = new Course[3];
for ( int i = 0; i < 3; i++)
Courses[i] = Non;
}

פורסם

קודם כל - אני חושב שההגדרה שלך ל Non בעזת define היא בעייתית, ושהיא "מחרבשת" לך את הקוד.

אל תשכח שקדם-המהדר (Pre-Compiler) הוא "טיפש", ובכל מקום שבו הוא מוצא את המילה Non - הוא מחליף אותה, בלי להבין את המשמעות שלה. אני לא חושב שאפשר לדעת לאיזה קוד הוא בדיוק מתרגם את הקוד שלך.

חוץ-מזה, הלולאה שמאפסת את המערך מיותרת, כי כבר השורה:

Courses = new Course[3];

מפעילה את בנאי-ברירת-המחדל של כל איברי המערך.

סתם הערות כללית :

למה האיבר Student::Courses הוא "ציבורי"?

למה המחלקה Course מוגדרת כ Struct?

למה לאתחל את Course::Course::number עם NULL, ולא ישר עם 0?

למה לאתחל את Course::Course::name עם "NULL", ולא ישר עם מחרוזת ריקה ("")?

למה המחלקה Course צריכה (בשלב זה) שיטה בונה-מעתיקה? (היא לא מחזיקה מצביעים, כרגע)

בהצלחה :xyxthumbs:

פורסם

ראשית ההרגלים שלך רעים. כדאי לך לקרוא coding guidlines כדי לקבל מושג ולדעת מה הסטנדרטים הנהוגים.

שים לב שאין לך include guards. יש לך define בקובץ ה-h והגדרה נוספת בקובץ ה-CPP. תבין מה הבעייתיות כאן.

ארכיון

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

דיונים חדשים

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.