Ho da poco iniziato a studiare le classi ed ho provato a scrivere questo programmino che gestisce con la classe point dei punti caratterizzati dalle cordinate x, y ed un nome.
L'unico problema che riscontro è nella funzione distance dove vorrei calcolare la distanza tra due punti. Questo è un estratto del codice, dove per l'altro è riscontrato l'errore dal compilatore:
L'errore è il seguente:codice:class point{ int x,y; string name; public: point(); point(int, int); void initWithValue(); void printPoint(); double distance(point par); }; point::point(){ x=0; y=0; name="Untitled"; } point::point(int a, int b){ x=a; y=b; name="Untitled"; } void point::initWithValue(){ int a, b; string n; cout << "Inserisci il nome del punto: "; getline(cin, n); cout << "Inserisci il valore delle ascisse: "; cin >> a; cout << "Inserisci il valore delle ordinate: "; cin >> b; x=a; y=b; name=n; } void point::printPoint(){ cout << "Punto: " << name << endl << "x: " << x << endl << "y: " << y << endl; } double point::distance(point par){ if(x.par==x){ if(x.par>=x) return(x.par-x); else return(x-x.par); } if(y.par==y){ if(y.par>=y){ return(y.par-y); } else return(y-y.par); } else{ return(sqrt((x.par+x)*(x.par+x)+(y.par+y)*(y.par+y))); } }
Grazie in anticipo dell'aiuto.codice:imac-di-Pacio:desktop pacio$ g++ program.cpp -o points program.cpp: In member function ‘double point::distance(point)’: program.cpp:56: error: request for member ‘par’ in ‘((point*)this)->point::x’, which is of non-class type ‘int’ program.cpp:57: error: request for member ‘par’ in ‘((point*)this)->point::x’, which is of non-class type ‘int’ program.cpp:59: error: request for member ‘par’ in ‘((point*)this)->point::x’, which is of non-class type ‘int’ program.cpp:61: error: request for member ‘par’ in ‘((point*)this)->point::y’, which is of non-class type ‘int’ program.cpp:62: error: request for member ‘par’ in ‘((point*)this)->point::y’, which is of non-class type ‘int’ program.cpp:63: error: request for member ‘par’ in ‘((point*)this)->point::y’, which is of non-class type ‘int’ program.cpp:66: error: request for member ‘par’ in ‘((point*)this)->point::y’, which is of non-class type ‘int’ program.cpp:69: error: request for member ‘par’ in ‘((point*)this)->point::x’, which is of non-class type ‘int’ program.cpp:69: error: request for member ‘par’ in ‘((point*)this)->point::x’, which is of non-class type ‘int’ program.cpp:69: error: request for member ‘par’ in ‘((point*)this)->point::y’, which is of non-class type ‘int’ program.cpp:69: error: request for member ‘par’ in ‘((point*)this)->point::y’, which is of non-class type ‘int’

Rispondi quotando