|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Aug 2004
Messaggi: 156
|
[C++] definizione di una semplice classe
Devo fare un semplice esercizio di c++ che mi chiede di implementare la classe Complex.
ho fatto questi due file complex.h Codice:
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex{
public:
Complex(float=0.0, float=0.0);
void setRe(float);
float getRe();
void setIm(float);
float getIm();
void print();
private:
float re; // parte reale
float im; // parte immaginaria
}
#endif
Codice:
#include <iostream>
#include "complex.h"
Complex::Complex(float r, float i){
setRe(r);
setIm(i);
}
void Complex::setRe(float r){
re = r;
}
void Complex::setIm(float i){
im = i;
}
float Complex::getRe(){
return re;
}
float Complex::getIm(){
return im;
}
void Complex::print(){
std::cout << "(" << re << "," << im << ")" ;
}
complex.cpp:4: error: new types may not be defined in a return type complex.cpp:4: error: return type specification for constructor invalid dove sbaglio? |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Apr 2003
Città: Genova
Messaggi: 4739
|
scusa ma c'è una cosa che non mi torna:
tu chiami funzioni membri della classe per implementare il costruttore...mentre se non sbaglio le funzioni Set e Get dovrebbero essere attive dopo che l' oggetto è stato istanziato, non prima... e infatti tutti i costruttori che ho visto erano della forma dato1(a); dato2(b)... ; in questo caso re(r);im(i); ...
__________________
Jappilas is a character created by a friend for his own comic - I feel honored he allowed me to bear his name Saber's true name belongs to myth - a Heroic Soul out of legends, fighting in our time to fullfill her only wish Let her image remind of her story, and of the emotions that flew from my heart when i assisted to her Fate
|
|
|
|
|
|
#3 | |
|
Member
Iscritto dal: Aug 2004
Messaggi: 156
|
Quote:
cmq ho ricompilato senza usare le set nel costruttore e mi da lo stesso problema.
|
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
C'era una volta un punto e virgola.
Il punto e virgola si sentiva minacciato dalla parentesi chiusa e decise quindi di lasciare la sua posizione e di andare a cercar fortuna per il mondo. |
|
|
|
|
|
#5 | |
|
Member
Iscritto dal: Aug 2004
Messaggi: 156
|
Quote:
|
|
|
|
|
|
|
#6 | |
|
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11794
|
Quote:
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
|
|
|
|
|
|
#7 | |
|
Member
Iscritto dal: Aug 2004
Messaggi: 156
|
Quote:
ma almeno la prossima volta non rimarrò un'ora a capire casa ho sbagliato ![]() spero
|
|
|
|
|
|
|
#8 | |
|
Senior Member
Iscritto dal: Oct 2002
Città: Roma
Messaggi: 1502
|
Quote:
e non capisco a che diavolo serva poi quel punto e virgola oltre a farti sbagliare ogni volta...è un esempio di ridondanza sintattica veramente inutile...
__________________
Sun Certified Java Programmer EUCIP Core Level Certified European Certification of Informatics Professionals |
|
|
|
|
|
|
#9 |
|
Member
Iscritto dal: Aug 2004
Messaggi: 156
|
Continuando l'esercizio ho scritto 2 metodi per fare la somma e la differenza
Codice:
Complex Complex::add(const Complex *b) const{
Complex res;
res.setRe(re + b->getRe());
res.setIm(im + b->getIm());
return res;
}
Complex Complex::subtract(const Complex &b) const{
Complex res;
res.setRe(re - b.getRe());
res.setIm(im - b.getIm());
return res;
}
c=a.add(&b); c=a.subtract(b); secondo voi è meglio usare i puntatori come in add o i riferimenti come in subtract? |
|
|
|
|
|
#10 | |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Quote:
class a { ... } pippo; |
|
|
|
|
|
|
#11 | |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Quote:
Se poi consideri che in questo caso dal puto di vista della traduzione in assembly sono esattamente equivalenti cosa preferisci ? |
|
|
|
|
|
|
#12 |
|
Senior Member
Iscritto dal: Apr 2003
Città: Genova
Messaggi: 4739
|
non facevi prima a ridefinirti gli operatori?
__________________
Jappilas is a character created by a friend for his own comic - I feel honored he allowed me to bear his name Saber's true name belongs to myth - a Heroic Soul out of legends, fighting in our time to fullfill her only wish Let her image remind of her story, and of the emotions that flew from my heart when i assisted to her Fate
|
|
|
|
|
|
#13 | |
|
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11794
|
Quote:
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
|
|
|
|
|
|
#14 | ||
|
Member
Iscritto dal: Aug 2004
Messaggi: 156
|
Quote:
Quote:
Codice:
Complex *Complex::subtract(const Complex &b) const{
Complex *res;
res->setRe(re - b.getRe());
res->setIm(im - b.getIm());
return res;
}
|
||
|
|
|
|
|
#15 | |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Quote:
|
|
|
|
|
|
|
#16 | |
|
Member
Iscritto dal: Aug 2004
Messaggi: 156
|
Quote:
questo invece dovrebbe andare bene Codice:
Complex *Complex::subtract(const Complex &b) const{
Complex *res = new Complex();
res->setRe(re - b.getRe());
res->setIm(im - b.getIm());
return res;
}
|
|
|
|
|
|
|
#17 | |
|
Senior Member
Iscritto dal: Apr 2003
Città: Genova
Messaggi: 4739
|
Quote:
Codice:
Complex operator+( Complex &altro ); Complex operator-( Complex &altro ); Codice:
Complex Complex::operator+( Complex & altro ){
return Complex( re + altro.re, im + altro.im );
};
Complex Complex::operator-( Complex & altro ){
return Complex( re - altro.re, im - altro.im );
};
in questo caso effettivamente crea un nuovo oggetto Complex e lo ritorna
__________________
Jappilas is a character created by a friend for his own comic - I feel honored he allowed me to bear his name Saber's true name belongs to myth - a Heroic Soul out of legends, fighting in our time to fullfill her only wish Let her image remind of her story, and of the emotions that flew from my heart when i assisted to her Fate
Ultima modifica di jappilas : 25-06-2005 alle 16:00. |
|
|
|
|
|
|
#18 | |
|
Senior Member
Iscritto dal: Apr 2003
Città: Genova
Messaggi: 4739
|
Quote:
al che chiedo (perchè ho il dubbio, non è un dom retorica) : non è codice a rischio di leak? stavo dando un' occhiata al criterio RAII, e mi pare la metodica sia differente...
__________________
Jappilas is a character created by a friend for his own comic - I feel honored he allowed me to bear his name Saber's true name belongs to myth - a Heroic Soul out of legends, fighting in our time to fullfill her only wish Let her image remind of her story, and of the emotions that flew from my heart when i assisted to her Fate
Ultima modifica di jappilas : 25-06-2005 alle 16:18. |
|
|
|
|
|
|
#19 | |
|
Member
Iscritto dal: Aug 2004
Messaggi: 156
|
Quote:
però in effetti io volgio creare un nuovo oggetto che è il risulatato di una operazione fara altri 2. Se metto un messaggio dentro il distruttore dell'oggetto riesco a capire quando viene distrutto? |
|
|
|
|
|
|
#20 | |
|
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11794
|
Quote:
"Resource Acquisition Is Initalisation" In italiano significa che chi crea un oggetto ne prende anche possesso ed e' il responsabile della sua distruzione. Se crei un oggetto Complex all'interno di un altro oggetto Complex dovresti anche mettere a disposizione i servizi per la sua distruzione. Prova questa implementazione: Codice:
Complex& Complex::subtract(const Complex &b) const{
setRe(re - b.getRe());
setIm(im - b.getIm());
return *this;
}
Codice:
Complex& Complex::subtract(Complex* result, Complex const& a, Complex const& b) const
{
result->setRe(a.getRe() - b.getRe());
result->setIm(b.getIm() - b.getIm());
return *res;
}
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 07:16.




















