Ver Mensaje Individual
  #1 (permalink)  
Antiguo 27/06/2011, 04:26
lexell
 
Fecha de Ingreso: abril-2007
Mensajes: 32
Antigüedad: 17 años, 6 meses
Puntos: 0
Pregunta fallos en mi código

Hola, estoy escribiendo un programa en C++ para la facultad, es de tema libre por lo que tengo libertad para cambiar las cosas, pero me da un error que no consigo solucionar por mucho que busco y cambio código, os dejo el código completo a ver si alguien me puede sacar de este apuro, muchas gracias por vuestra ayuda,

Código C++:
Ver original
  1. //.h
  2.  
  3. #ifndef _PROJ_
  4. #define _PROJ_
  5.  
  6. #include <string>
  7. #include <iostream>
  8.  
  9. struct vehi
  10. {
  11.     public:
  12.     virtual void print (std::ostream& stream)const=0;
  13.     virtual vehi * clone () const=0;
  14.     ~vehi(){}
  15. }
  16.  
  17. struct vehicle
  18. {
  19.     public:
  20.     vehi* ref;
  21.    
  22.     vehicle (const vehicle& s ) : ref (s.ref->clone()){}
  23.   vehicle ( const vehi& s) : ref(s.clone()){}
  24.  
  25.   void operator =  (const vehicle& s)
  26.   {
  27.     if (s.ref != ref)
  28.     {
  29.       delete ref;
  30.       ref = s.ref->clone();
  31.     }
  32.   }
  33.  
  34.   void operator = ( const vehi& s)
  35.   {
  36.     if (&s != ref)
  37.     {
  38.       delete ref;
  39.       ref = s.clone();
  40.     }
  41.   }
  42.  
  43.   ~vehicle()
  44.   {
  45.     delete ref;
  46.   }
  47.  
  48. }
  49.  
  50. struct car : public vehi
  51. {
  52.     public:
  53.   std::string type;
  54.   float weight;
  55.     unsigned int wheel;
  56.   unsigned int passanger;
  57.   unsigned int motor;
  58.  
  59.     car () : wheel(0),passanger(0),motor(0),type(""),weight(0.0){}
  60.    
  61.   car(unsigned int wheel, unsigned int passanger, unsigned int motor, std::string type, float weight)
  62.     : wheel(wheel),passanger (passanger),motor (motor), type(type), weight(weight){}
  63.    
  64.   ~car(){}
  65.  
  66.   virtual vehi * clone () const
  67.   {
  68.     return new car(*this);
  69.   }
  70.  
  71.   virtual void print( std::ostream& ) const
  72.   {
  73.     std::cout << "CAR:\n\nWheel: " << wheel << "\nPassanger: "<< passanger << "\nMotor: "<< motor <<"\nType: " << type << "\nWeight: " << weight << "\n\n";
  74.   }
  75. }
  76.  
  77. struct motorbike : public vehi
  78. {
  79.     unsigned int wheel;
  80.   unsigned int passanger;
  81.   unsigned int motor;
  82.   std::string type;
  83.   std::string brand;
  84.  
  85.     motorbike () : wheel(0),passanger(0),motor(0),type(""),brand(""){}
  86.    
  87.   motorbike(unsigned int wheel, unsigned int passanger, unsigned int motor, std::string type, std::string brand)
  88.     : wheel(wheel),passanger (passanger),motor (motor), type(type), brand(brand){}
  89.    
  90.   ~motorbike(){}
  91.  
  92.   virtual vehi * clone () const
  93.   {
  94.     return new motorbike(*this);
  95.   }
  96.  
  97.   virtual void print( std::ostream& ) const
  98.   {
  99.     std::cout << "MOTORBIKE:\n\nWheel: " << wheel << "\nPassanger: "<< passanger << "\nMotor: "<< motor <<"\nType: " << type << "\nBrand: " << brand << "\n\n";
  100.   }
  101. }
  102.  
  103. struct truck : public vehi
  104. {
  105.     unsigned int wheel;
  106.   unsigned int passanger;
  107.   unsigned int motor;
  108.   unsigned int charge;
  109.   float weight;
  110.  
  111.     truck () : wheel(0),passanger(0),motor(0),charge(0),weight(0){}
  112.    
  113.   truck(unsigned int wheel, unsigned int passanger, unsigned int motor, unsigned int charge, float weight)
  114.     : wheel(wheel),passanger (passanger),motor (motor), charge(charge), weight(weight){}
  115.    
  116.   ~truck(){}
  117.  
  118.   virtual vehi * clone () const
  119.   {
  120.     return new truck(*this);
  121.   }
  122.  
  123.  
  124.   virtual void print( std::ostream& ) const
  125.   {
  126.     std::cout << "TRUCK:\n\nWheel: " << wheel << "\nPassanger: "<< passanger << "\nMotor: "<< motor <<"\nCharge: " << charge << "\nWeight: " << weight << "\n\n";
  127.   }
  128. }
  129.  
  130.  
  131. std::ostream& operator << (std::ostream& stream, const vehicle& ve)
  132. {
  133.   ve.ref->print (stream);
  134.   return (stream);
  135. }
  136.  
  137.  
  138. #endif
  139.  
  140.  
  141.  
  142. //.cpp
  143.  
  144. int main( int argc, char* argv [ ] )
  145. {
  146.     std::list< vehicle > l;
  147.   car c (4,5,1600,"tourism",1200);
  148.     motorbike m (2,2,600,"race","Honda");
  149.     truck t (8,2,5000,10000,8000);
  150.  
  151.  
  152.   l.push_back(c);
  153.   l.push_back(m);
  154.   l.push_back(t);
  155.  
  156.   for( std::list< vehicle > :: const_iterator
  157.         p = l.begin( );
  158.         p != l.end( );
  159.         ++ p )
  160.   {
  161.     std::cout << *p << "\n";
  162.   }
  163. }


Errores:

proj.h:146: error: expected initializer before ‘&’ token
proj.cpp: In function ‘int main(int, char**)’:
proj.cpp:22: error: no match for ‘operator<<’ in ‘std::cout << p.std::_List_const_iterator<_Tp>::operator* [with _Tp = vehicle]()’

bueno por ahora este es mi codigo, espero que me podais ayudar, muchas gracias de antemano

salu2!!