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

עזרה עם Templates ב C++.

Featured Replies

פורסם

שלום לכולם.

נתקלתי בבעיית קומפילציה שאינני מצליח להתגבר עליה:

לפניכם שלושה קטעי קוד:

#include <iostream.h>


template <class X, class Y>
class Client
{
private:
X  x;                 
Y  y;

public :
Client(X a=X(), Y b=Y());
Client & operator = (const X &);
Client & operator = (const Y &);
Client & operator = (const Client &);
bool operator == (const X &) const;
bool operator == (const Y &) const;
bool operator == (const Client &) const;
};

template <class X, class Y>
Client<X, Y>::Client(X a, Y b)
{
x=a;
y=b;
}

template <class X, class Y>
Client<X, Y> & Client<X, Y>::operator = (const X &a)
{
x=a;
return *this;
}

template <class X, class Y>
Client<X, Y> & Client<X, Y>::operator = (const Y &b)
{
y=b;
return *this;
}

template <class X, class Y>
Client<X, Y> & Client<X, Y>::operator = (const Client &c)
{
x=c.x;
y=c.y;
return *this;
}

template <class X, class Y>
bool Client<X, Y>::operator == (const X &a) const
{
if (x==a)
return 1;
else
return 0;
}

template <class X, class Y>
bool Client<X, Y>::operator == (const Y &b) const
{
if (y==b)
return 1;
else
return 0;
}

template <class X, class Y>
bool Client<X, Y>::operator == (const Client &c) const
{
if ((x=c.x) && (y=c.y))
return 1;
else
return 0;
}

#include "Client.h"


template <class X, class Y, int size>
class TelBook
{
private:
Client<X, Y> arr[size];

public:
TelBook ();
TelBook (Client<X, Y> [size]);
Client<X, Y> & operator [] (int);
TelBook & operator = (TelBook &);
int operator == (const X &) const;
int operator == (const Y &) const;
int operator == (const Client <X, Y> &) const;
};

template <class X, class Y, int size>
TelBook<X, Y, size>::TelBook ()
{
for (int i=0; i<size; i++)
arr[i]=Client();
}

template <class X, class Y, int size>
TelBook<X, Y, size>::TelBook (Client<X, Y> temp[size])
{
for (int i=0; i<size; i++)
arr[i]=temp[i];
}

template <class X, class Y, int size>
Client<X, Y> & TelBook<X, Y, size>::operator [] (int i)
{
if ((0<=i) && (i<size))
return arr[i];
}

template <class X, class Y, int size>
TelBook<X, Y, size> & TelBook<X, Y, size>::operator = (TelBook &tb)
{
for (int i=0; i<size; i++)
arr[i]=tb.arr[i];
return *this;
}

template <class X, class Y, int size>
int TelBook<X, Y, size>::operator == (const X &a) const
{
for (int i=0; i<size; i++)
if (arr[i]==a)
return i;
return -1;
}

template <class X, class Y, int size>
int TelBook<X, Y, size>::operator == (const Y &b) const
{
for (int i=0; i<size; i++)
if (arr[i]==b)
return i;
return -1;
}

template <class X, class Y, int size>
int TelBook<X, Y, size>::operator == (const Client <X, Y> &c) const
{
for (int i=0; i<size; i++)
if (arr[i]==c)
return i;
return -1;
}

#include "TelBook.h"
#include <string.h>

#define SIZE 3

int main()
{
char name[5]="rami";
Client<long, char[5]> C1(12345, "mosh"), C2(8824892, "kobi"), C3(8865423, "rami");
Client<long, char[5]> arrClients[SIZE]={C1, C2, C3};
TelBook<long, char[5], SIZE> tb(arrClients);
int i=tb==name;
cout<<i<<endl;
return 0;
}

בעת קימפול התוכנית, הקומפיילר של Visual C++:

Compiling...

main.cpp

e:\university\adt\ex4\ex3\client.h(25) : error C2440: '=' : cannot convert from 'char []' to 'char [5]'

        There are no conversions to array types, although there are conversions to references or pointers to arrays

        E:\University\ADT\Ex4\Ex3\main.cpp(16) : while compiling class-template member function '__thiscall Client<long,char [5]>::Client<long,char [5]>(long,char [])'

Error executing cl.exe.

main.obj - 1 error(s), 0 warning(s)

הקומפיילר מציין בעייה בשורה 25 בקוד הראשון, שהיא y=b; בפונקציה:

template <class X, class Y>
Client<X, Y>::Client(X a, Y b)
{
x=a;
y=b;
}

תודה רבה על כל עזרה!

בתודה מראש, קובי.

פורסם

אם גם הפרמטר הוא Client<X,Y> אתה צריך לכתוב אותו שם, כלומר

template <class X, class Y>
Client<X, Y> & Client<X, Y>::operator = (const Client<X,Y> &c)
{
x=c.x;
y=c.y;
return *this;
}

זה הופיע אצלך בעוד כמה מקומות.

פורסם
  • מחבר

לא הבנתי.

אין פרמטר Client<X, Y> בקונסטרקטור...

הקומפיילר מציין בעייה של העברת char[] ל char[5], לא הבנתי איפה הוא מצא בתוכנית טיפוס מסוג char[]...

בתודה מראש על כל עזרה, קובי.

פורסם

דיברתי על אופרטור ההשמה (למרות שאותו דבר נמצא גם ב- operator== ).

לא קיים class Client, לכן בכל מקום שבו כתוב Client חייב להיות Client<X,Y>

פורסם
  • מחבר

צודק, תיקנתי.

אך, הבעייה הראשונה עדיין לא נפתרה...

בתודה מראש, קובי.

פורסם

הקומפיילר מציין בעייה של העברת char[] ל char[5], לא הבנתי איפה הוא מצא בתוכנית טיפוס מסוג char[]...

המחרוזות "rami", "mosh" ו-"kobi" הן מהסוג הזה.

פורסם

לא נראה לי שדבר כזה

template <class X, class Y, int size>

TelBook<X, Y, size>::TelBook (Client<X, Y> temp)

יכול לעבוד.

למה בכלל אתה מעביר מערכים על ה- stack?! זה רע (ואין לי כוח להסביר עוד פעם למה)

תעביר char*, ואם אתה צריך - תעתיק אותו בתוך הפונקציה.

עוד אופציה זה להשתמש ב- string.

ארכיון

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

דיונים חדשים

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.