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//.h
#ifndef _PROJ_
#define _PROJ_
#include <string>
#include <iostream>
struct vehi
{
public:
virtual void print (std::ostream& stream)const=0;
virtual vehi * clone () const=0;
~vehi(){}
}
struct vehicle
{
public:
vehi* ref;
vehicle (const vehicle& s ) : ref (s.ref->clone()){}
vehicle ( const vehi& s) : ref(s.clone()){}
void operator = (const vehicle& s)
{
if (s.ref != ref)
{
delete ref;
ref = s.ref->clone();
}
}
void operator = ( const vehi& s)
{
if (&s != ref)
{
delete ref;
ref = s.clone();
}
}
~vehicle()
{
delete ref;
}
}
struct car : public vehi
{
public:
std::string type;
float weight;
unsigned int wheel;
unsigned int passanger;
unsigned int motor;
car () : wheel(0),passanger(0),motor(0),type(""),weight(0.0){}
car(unsigned int wheel, unsigned int passanger, unsigned int motor, std::string type, float weight)
: wheel(wheel),passanger (passanger),motor (motor), type(type), weight(weight){}
~car(){}
virtual vehi * clone () const
{
return new car(*this);
}
virtual void print( std::ostream& ) const
{
std::cout << "CAR:\n\nWheel: " << wheel << "\nPassanger: "<< passanger << "\nMotor: "<< motor <<"\nType: " << type << "\nWeight: " << weight << "\n\n";
}
}
struct motorbike : public vehi
{
unsigned int wheel;
unsigned int passanger;
unsigned int motor;
std::string type;
std::string brand;
motorbike () : wheel(0),passanger(0),motor(0),type(""),brand(""){}
motorbike(unsigned int wheel, unsigned int passanger, unsigned int motor, std::string type, std::string brand)
: wheel(wheel),passanger (passanger),motor (motor), type(type), brand(brand){}
~motorbike(){}
virtual vehi * clone () const
{
return new motorbike(*this);
}
virtual void print( std::ostream& ) const
{
std::cout << "MOTORBIKE:\n\nWheel: " << wheel << "\nPassanger: "<< passanger << "\nMotor: "<< motor <<"\nType: " << type << "\nBrand: " << brand << "\n\n";
}
}
struct truck : public vehi
{
unsigned int wheel;
unsigned int passanger;
unsigned int motor;
unsigned int charge;
float weight;
truck () : wheel(0),passanger(0),motor(0),charge(0),weight(0){}
truck(unsigned int wheel, unsigned int passanger, unsigned int motor, unsigned int charge, float weight)
: wheel(wheel),passanger (passanger),motor (motor), charge(charge), weight(weight){}
~truck(){}
virtual vehi * clone () const
{
return new truck(*this);
}
virtual void print( std::ostream& ) const
{
std::cout << "TRUCK:\n\nWheel: " << wheel << "\nPassanger: "<< passanger << "\nMotor: "<< motor <<"\nCharge: " << charge << "\nWeight: " << weight << "\n\n";
}
}
std::ostream& operator << (std::ostream& stream, const vehicle& ve)
{
ve.ref->print (stream);
return (stream);
}
#endif
//.cpp
int main( int argc, char* argv [ ] )
{
std::list< vehicle > l;
car c (4,5,1600,"tourism",1200);
motorbike m (2,2,600,"race","Honda");
truck t (8,2,5000,10000,8000);
l.push_back(c);
l.push_back(m);
l.push_back(t);
for( std::list< vehicle > :: const_iterator
p = l.begin( );
p != l.end( );
++ p )
{
std::cout << *p << "\n";
}
}
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!!