problem with class and sub-class

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 25
Reputation: onemanclapping is an unknown quantity at this point 
Solved Threads: 0
onemanclapping onemanclapping is offline Offline
Light Poster

problem with class and sub-class

 
0
  #1
Sep 30th, 2008
hi, I'm a Computer Engineering student from Portugal in the 2ºterm.

Last class' job was for us to use class and sub-class systems, creating a info system for vehicle types.

I thought I was finished, but when I did the main file, the compiler shouted loads of errors that didn't happen before I'd written my main file.

here's the compiler shouting:
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
../main.cpp: In function ‘int main()’:
../main.cpp:21: error: cannot declare variable ‘b1’ to be of abstract type ‘Bicicleta’
../Bicicleta.h:7: note: because the following virtual functions are pure within ‘Bicicleta’:
../Veiculo.h:18: note: virtual float Veiculo::calcImposto() const
../main.cpp:29: warning: comparison between signed and unsigned integer expressions
../main.cpp:38: warning: comparison between signed and unsigned integer expressions
../Veiculo.h: In member function ‘Veiculo& Veiculo::operator=(const Veiculo&)’:
../Veiculo.h:12: instantiated from ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Motorizado, _Alloc = std::allocator<Motorizado>]’
/usr/include/c++/4.2/bits/stl_vector.h:605: instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Motorizado, _Alloc = std::allocator<Motorizado>]’
../main.cpp:33: instantiated from here
../Veiculo.h:12: error: non-static const member ‘const std::string Veiculo::marca’, can't use default assignment operator
../Veiculo.h:12: error: non-static const member ‘const int Veiculo::mes’, can't use default assignment operator
../Veiculo.h:12: error: non-static const member ‘const int Veiculo::ano’, can't use default assignment operator
../Motorizado.h: In member function ‘Motorizado& Motorizado::operator=(const Motorizado&)’:
../Motorizado.h:7: note: synthesized method ‘Veiculo& Veiculo::operator=(const Veiculo&)’ first required here
../Motorizado.h:7: error: non-static const member ‘const std::string Motorizado::combustivel’, can't use default assignment operator
../Motorizado.h:7: error: non-static const member ‘const int Motorizado::cilindrada’, can't use default assignment operator
/usr/include/c++/4.2/bits/vector.tcc: In member function ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Motorizado, _Alloc = std::allocator<Motorizado>]’:
/usr/include/c++/4.2/bits/vector.tcc:256: note: synthesized method ‘Motorizado& Motorizado::operator=(const Motorizado&)’ first required here
make: *** [main.o] Error 1
make: Target `all' not remade because of errors.
Build complete for project TP2
here are the code files(also included as attachments):

main.cpp
  1. #include "Automovel.h"
  2. #include "Bicicleta.h"
  3. #include "Camiao.h"
  4. #include "Motorizado.h"
  5. #include "Veiculo.h"
  6. #include <iostream>
  7. #include <vector>
  8.  
  9.  
  10.  
  11. using namespace std;
  12.  
  13.  
  14. int main()
  15. {
  16. vector<Veiculo*> v;
  17.  
  18. Automovel a1("Fiat",5,1997,"gasolina",1200,5);
  19. Camiao c1("Ford",3,1999,"gasolina",1400,1000);
  20. Automovel a2("Renault",9,1997,"gasoleo",1300,5);
  21. Bicicleta b1("XX",4,2002,"TodoTerreno");
  22.  
  23. v.push_back(&a1);
  24. v.push_back(&c1);
  25. v.push_back(&a2);
  26. v.push_back(&b1);
  27.  
  28. cout << "vector v:"<< endl;
  29. for (int i=0; i<v.size(); i++)
  30. v[i]->info();
  31.  
  32. vector<Motorizado> v2;
  33. v2.push_back(a1);
  34. v2.push_back(c1);
  35. v2.push_back(a2);
  36.  
  37. cout << "vector v2:"<< endl;
  38. for (int i=0; i<v2.size(); i++)
  39. v2[i].info();
  40. }

Automovel.h
  1. #ifndef AUTOMOVEL_H_
  2. #define AUTOMOVEL_H_
  3.  
  4. #include "Motorizado.h"
  5.  
  6. class Automovel : public Motorizado
  7. {
  8. public:
  9. Automovel(string m, int mm, int aaaa, string comb, int cil, int lug);
  10. virtual ~Automovel();
  11. void info() const;
  12. private:
  13. const int lugares;
  14. };
  15.  
  16. #endif /*AUTOMOVEL_H_*/

Automovel.cpp
  1. #include "Automovel.h"
  2.  
  3. Automovel::Automovel(string m, int mm, int aaaa, string comb, int cil, int lug)
  4. :Motorizado(m,mm,aaaa,comb,cil),lugares(lug)
  5. {
  6. }
  7.  
  8. Automovel::~Automovel()
  9. {
  10. }
  11.  
  12. void Automovel::info() const
  13. {
  14. Motorizado::info();
  15. cout << "Lugares: " << lugares << endl;
  16. }

Bicicleta.h
  1. #ifndef BICICLETA_H_
  2. #define BICICLETA_H_
  3.  
  4. #include "Veiculo.h"
  5.  
  6. class Bicicleta : public Veiculo
  7. {
  8. public:
  9. Bicicleta(string m, int aaaa, int mm, string t);
  10. virtual ~Bicicleta();
  11. void info() const;
  12. private:
  13. const string tipo;
  14. };
  15.  
  16. #endif /*BICICLETA_H_*/

Bicicleta.cpp
  1. #include "Bicicleta.h"
  2.  
  3. Bicicleta::Bicicleta(string m, int aaaa, int mm, string t)
  4. :Veiculo(m,aaaa,mm),tipo(t)
  5. {
  6. }
  7.  
  8. Bicicleta::~Bicicleta()
  9. {
  10. }
  11.  
  12. void Bicicleta::info() const
  13. {
  14. Veiculo::info();
  15. cout << "Tipo: " << tipo << cout;
  16. }

Camiao.h
  1. #ifndef CAMIAO_H_
  2. #define CAMIAO_H_
  3.  
  4. #include "Motorizado.h"
  5.  
  6. class Camiao : public Motorizado
  7. {
  8. public:
  9. Camiao(string m, int mm, int aaaa, string comb, int cil, int p);
  10. virtual ~Camiao();
  11. void info() const;
  12. private:
  13. const int peso;
  14. };
  15.  
  16. #endif /*CAMIAO_H_*/

Camiao.cpp
  1. #include "Camiao.h"
  2.  
  3. Camiao::Camiao(string m, int mm, int aaaa, string comb, int cil, int p)
  4. :Motorizado(m,mm,aaaa,comb,cil),peso(p)
  5. {
  6. }
  7.  
  8. Camiao::~Camiao()
  9. {
  10. }
  11.  
  12. void Camiao::info() const
  13. {
  14. Motorizado::info();
  15. cout << "Peso: " << peso << endl;
  16. }

Motorizado.h
  1. #ifndef MOTORIZADO_H_
  2. #define MOTORIZADO_H_
  3.  
  4. #include "Veiculo.h"
  5.  
  6. class Motorizado : public Veiculo
  7. {
  8. public:
  9. Motorizado(string m, int mm, int aaaa, string comb, int cil);
  10. virtual ~Motorizado();
  11. void info() const;
  12. float calcImposto() const;
  13. private:
  14. const string combustivel;
  15. const int cilindrada;
  16. };
  17.  
  18. #endif /*MOTORIZADO_H_*/

Motorizado.cpp
  1. #include "Motorizado.h"
  2.  
  3. Motorizado::Motorizado(string m, int mm, int aaaa, string comb, int cil)
  4. :Veiculo(m,aaaa,mm),combustivel(comb),cilindrada(cil)
  5. {
  6. }
  7.  
  8. Motorizado::~Motorizado()
  9. {
  10. }
  11.  
  12. void Motorizado::info() const
  13. {
  14. Veiculo::info();
  15. cout << "Combustivel: " << combustivel << endl;
  16. cout << "Cilindrada: " << cilindrada << endl;
  17. }
  18.  
  19. float Motorizado::calcImposto() const
  20. {
  21. if(ano>1995)
  22. {
  23. if((combustivel=="gasolina"&&cilindrada<=1000)||(combustivel!="gasolina"&&cilindrada<=1500))
  24. return 14,56;
  25. if((combustivel=="gasolina"&&cilindrada>1000&&cilindrada<=1300)||(combustivel!="gasolina"&&cilindrada>1500&&cilindrada<=2000))
  26. return 29,06;
  27. if((combustivel=="gasolina"&&cilindrada>1300&&cilindrada<=1750)||(combustivel!="gasolina"&&cilindrada>2000&&cilindrada<=3000))
  28. return 45,15;
  29. if((combustivel=="gasolina"&&cilindrada>1750&&cilindrada<=2600)||(combustivel!="gasolina"&&cilindrada>3000))
  30. return 113,98;
  31. if(combustivel=="gasolina"&&cilindrada>2600&&cilindrada<=3500)
  32. return 181,17;
  33. if(combustivel=="gasolina"&&cilindrada>3500)
  34. return 320,89;
  35. }
  36. else
  37. {
  38. if((combustivel=="gasolina"&&cilindrada<=1000)||(combustivel!="gasolina"&&cilindrada<=1500))
  39. return 8,10;
  40. if((combustivel=="gasolina"&&cilindrada>1000&&cilindrada<=1300)||(combustivel!="gasolina"&&cilindrada>1500&&cilindrada<=2000))
  41. return 14,56;
  42. if((combustivel=="gasolina"&&cilindrada>1300&&cilindrada<=1750)||(combustivel!="gasolina"&&cilindrada>2000&&cilindrada<=3000))
  43. return 22,65;
  44. if((combustivel=="gasolina"&&cilindrada>1750&&cilindrada<=2600)||(combustivel!="gasolina"&&cilindrada>3000))
  45. return 54,89;
  46. if(combustivel=="gasolina"&&cilindrada>2600&&cilindrada<=3500)
  47. return 87,13;
  48. if(combustivel=="gasolina"&&cilindrada>3500)
  49. return 148,37;
  50. }
  51. }

Veiculo.h
  1. #ifndef VEICULO_H_
  2. #define VEICULO_H_
  3.  
  4. #include <string>
  5. using std::string;
  6.  
  7. #include <iostream>
  8. using std::cout;
  9. using std::endl;
  10.  
  11. class Veiculo
  12. {
  13. public:
  14. Veiculo(string m, int aaaa, int mm);
  15. virtual ~Veiculo();
  16. virtual void info() const;
  17. bool operator < (const Veiculo &v1) const;
  18. virtual float calcImposto() const = 0;
  19.  
  20. private:
  21. const string marca;
  22. const int mes;
  23.  
  24. protected:
  25. const int ano;
  26. };
  27.  
  28. #endif /*VEICULO_H_*/

Veiculo.cpp
  1. #include "Veiculo.h"
  2.  
  3. Veiculo::Veiculo(string m, int aaaa, int mm):marca(m), ano(aaaa), mes(mm)
  4. {
  5. }
  6.  
  7. Veiculo::~Veiculo()
  8. {
  9. }
  10.  
  11. void Veiculo::info() const
  12. {
  13. cout << "Marca: " << marca << endl;
  14. cout << "Ano:" << ano << " Mês:" << mes << endl;
  15. }
  16.  
  17. bool Veiculo::operator < (const Veiculo &v1) const
  18. {
  19. if(ano>v1.ano || (ano==v1.ano && mes>v1.mes))
  20. return true;
  21. return false;
  22. }

thank you very much in advance and sorry for the disturb, but I think it should not be a big error ;/
Attached Files
File Type: zip prog.zip (3.8 KB, 3 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: problem with class and sub-class

 
0
  #2
Oct 1st, 2008
class Bicicleta must implement all pure virtual methods of its base class before it can be instiantiated. It does not do that, hence the error message. Add the function virtual float calcImposto() const; (identical to the one in Veiculo but without the =0 part) to Biciclets and it should resolve the problem.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 8
Reputation: koteswarvijay is an unknown quantity at this point 
Solved Threads: 2
koteswarvijay's Avatar
koteswarvijay koteswarvijay is offline Offline
Newbie Poster

Re: problem with class and sub-class

 
0
  #3
Oct 1st, 2008
Try this code in "Bicicleta.cpp"
float Bicicleta::calcImposto() const
{
}

and in "Bicicleta.h"
float calcImposto() const;

There entries should be there as it was defined as Virtual in the Base class and the derived class has to write the definition for the same.

This should work. Any more problems, please let me know.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 25
Reputation: onemanclapping is an unknown quantity at this point 
Solved Threads: 0
onemanclapping onemanclapping is offline Offline
Light Poster

Re: problem with class and sub-class

 
0
  #4
Oct 1st, 2008
Originally Posted by Ancient Dragon View Post
class Bicicleta must implement all pure virtual methods of its base class before it can be instiantiated. It does not do that, hence the error message. Add the function virtual float calcImposto() const; (identical to the one in Veiculo but without the =0 part) to Biciclets and it should resolve the problem.
oooh! that's it! thank you very much it's working fine now!

best regards!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC