עבור לתוכן
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.

בעייה העמסת אופרטור <<cout ב- C++

Featured Replies

פורסם

אינני מבין מדוע אם ההעמסה היא ידידת המחלקה אז יש "התנגשות במשמעות של האופרטור" ואילו אם אני מבטל אותה כידידה אז זה עובד

#include <iostream>
using namespace std;

class av {
int b;
public:
void print() {cout<<b;}
void setb(int x) {b=x;}
//friend ostream &operator<<(ostream &stream, av obj);

};
ostream &operator<<(ostream &stream, av obj)
{
obj.print();
return stream;
}
int main ()
{
av bob;
bob.setb(8);
cout<<bob;
return 0;
}

פורסם

נראה לי יותר נכון לכתוב ככה, לא ? (שים לב להעברת האוביקט כרפרנס ב >>)

#include <iostream>
using namespace std;

class av {
int b;
public:
void setb(int x) {b=x;}
friend ostream &operator<<(ostream &stream, const av &obj);
};
ostream &operator<<(ostream &stream, const av &obj)
{
stream << obj.b;
return stream;
}
int main ()
{
av bob;
bob.setb(8);
cout<<bob;
return 0;
}

פורסם

אינני מבין מדוע אם ההעמסה היא ידידת המחלקה אז יש "התנגשות במשמעות של האופרטור" ואילו אם אני מבטל אותה כידידה אז זה עובד

הרגע ניסיתי עם GCC, ולא נראה שיש איזושהי בעיה כשמשתמשים ב-friend. באיזה קומפיילר אתה עובד?

בכל מקרה, sdfzxc צודק - תעביר את האוביקט כ-const reference. תוודא שהחתימה (prototype) של האופרטור זהה בשני המקומות, אחרת סביר שתקבל שגיאה.

ארכיון

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

דיונים חדשים

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.