פורסם 2005 ביוני 1220 שנים שלום לכולם.נתקלתי בבעיית קומפילציה שאינני מצליח להתגבר עליה:לפניכם שלושה קטעי קוד:#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 3int 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.cppe:\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;}תודה רבה על כל עזרה!בתודה מראש, קובי.
פורסם 2005 ביוני 1220 שנים אם גם הפרמטר הוא 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;}זה הופיע אצלך בעוד כמה מקומות.
פורסם 2005 ביוני 1220 שנים מחבר לא הבנתי.אין פרמטר Client<X, Y> בקונסטרקטור...הקומפיילר מציין בעייה של העברת char[] ל char[5], לא הבנתי איפה הוא מצא בתוכנית טיפוס מסוג char[]...בתודה מראש על כל עזרה, קובי.
פורסם 2005 ביוני 1220 שנים דיברתי על אופרטור ההשמה (למרות שאותו דבר נמצא גם ב- operator== ).לא קיים class Client, לכן בכל מקום שבו כתוב Client חייב להיות Client<X,Y>
פורסם 2005 ביוני 1220 שנים הקומפיילר מציין בעייה של העברת char[] ל char[5], לא הבנתי איפה הוא מצא בתוכנית טיפוס מסוג char[]...המחרוזות "rami", "mosh" ו-"kobi" הן מהסוג הזה.
פורסם 2005 ביוני 1220 שנים לא נראה לי שדבר כזהtemplate <class X, class Y, int size>TelBook<X, Y, size>::TelBook (Client<X, Y> temp)יכול לעבוד.למה בכלל אתה מעביר מערכים על ה- stack?! זה רע (ואין לי כוח להסביר עוד פעם למה)תעביר char*, ואם אתה צריך - תעתיק אותו בתוך הפונקציה.עוד אופציה זה להשתמש ב- string.
ארכיון
דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.