943,722 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1656
  • C++ RSS
Sep 30th, 2008
0

problem with class and sub-class

Expand Post »
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:
Quote ...
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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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, 21 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
onemanclapping is offline Offline
25 posts
since Feb 2008
Oct 1st, 2008
0

Re: problem with class and sub-class

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Oct 1st, 2008
0

Re: problem with class and sub-class

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.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
koteswarvijay is offline Offline
8 posts
since Sep 2008
Oct 1st, 2008
0

Re: problem with class and sub-class

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!
Reputation Points: 10
Solved Threads: 0
Light Poster
onemanclapping is offline Offline
25 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: file handling problem
Next Thread in C++ Forum Timeline: Problem in homework (new to C++)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC