| | |
problem with class and sub-class
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 25
Reputation:
Solved Threads: 0
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:
here are the code files(also included as attachments):
main.cpp
Automovel.h
Automovel.cpp
Bicicleta.h
Bicicleta.cpp
Camiao.h
Camiao.cpp
Motorizado.h
Motorizado.cpp
Veiculo.h
Veiculo.cpp
thank you very much in advance and sorry for the disturb, but I think it should not be a big error ;/
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
main.cpp
C++ Syntax (Toggle Plain Text)
#include "Automovel.h" #include "Bicicleta.h" #include "Camiao.h" #include "Motorizado.h" #include "Veiculo.h" #include <iostream> #include <vector> using namespace std; int main() { vector<Veiculo*> v; Automovel a1("Fiat",5,1997,"gasolina",1200,5); Camiao c1("Ford",3,1999,"gasolina",1400,1000); Automovel a2("Renault",9,1997,"gasoleo",1300,5); Bicicleta b1("XX",4,2002,"TodoTerreno"); v.push_back(&a1); v.push_back(&c1); v.push_back(&a2); v.push_back(&b1); cout << "vector v:"<< endl; for (int i=0; i<v.size(); i++) v[i]->info(); vector<Motorizado> v2; v2.push_back(a1); v2.push_back(c1); v2.push_back(a2); cout << "vector v2:"<< endl; for (int i=0; i<v2.size(); i++) v2[i].info(); }
Automovel.h
C++ Syntax (Toggle Plain Text)
#ifndef AUTOMOVEL_H_ #define AUTOMOVEL_H_ #include "Motorizado.h" class Automovel : public Motorizado { public: Automovel(string m, int mm, int aaaa, string comb, int cil, int lug); virtual ~Automovel(); void info() const; private: const int lugares; }; #endif /*AUTOMOVEL_H_*/
Automovel.cpp
C++ Syntax (Toggle Plain Text)
#include "Automovel.h" Automovel::Automovel(string m, int mm, int aaaa, string comb, int cil, int lug) :Motorizado(m,mm,aaaa,comb,cil),lugares(lug) { } Automovel::~Automovel() { } void Automovel::info() const { Motorizado::info(); cout << "Lugares: " << lugares << endl; }
Bicicleta.h
C++ Syntax (Toggle Plain Text)
#ifndef BICICLETA_H_ #define BICICLETA_H_ #include "Veiculo.h" class Bicicleta : public Veiculo { public: Bicicleta(string m, int aaaa, int mm, string t); virtual ~Bicicleta(); void info() const; private: const string tipo; }; #endif /*BICICLETA_H_*/
Bicicleta.cpp
C++ Syntax (Toggle Plain Text)
#include "Bicicleta.h" Bicicleta::Bicicleta(string m, int aaaa, int mm, string t) :Veiculo(m,aaaa,mm),tipo(t) { } Bicicleta::~Bicicleta() { } void Bicicleta::info() const { Veiculo::info(); cout << "Tipo: " << tipo << cout; }
Camiao.h
C++ Syntax (Toggle Plain Text)
#ifndef CAMIAO_H_ #define CAMIAO_H_ #include "Motorizado.h" class Camiao : public Motorizado { public: Camiao(string m, int mm, int aaaa, string comb, int cil, int p); virtual ~Camiao(); void info() const; private: const int peso; }; #endif /*CAMIAO_H_*/
Camiao.cpp
C++ Syntax (Toggle Plain Text)
#include "Camiao.h" Camiao::Camiao(string m, int mm, int aaaa, string comb, int cil, int p) :Motorizado(m,mm,aaaa,comb,cil),peso(p) { } Camiao::~Camiao() { } void Camiao::info() const { Motorizado::info(); cout << "Peso: " << peso << endl; }
Motorizado.h
C++ Syntax (Toggle Plain Text)
#ifndef MOTORIZADO_H_ #define MOTORIZADO_H_ #include "Veiculo.h" class Motorizado : public Veiculo { public: Motorizado(string m, int mm, int aaaa, string comb, int cil); virtual ~Motorizado(); void info() const; float calcImposto() const; private: const string combustivel; const int cilindrada; }; #endif /*MOTORIZADO_H_*/
Motorizado.cpp
C++ Syntax (Toggle Plain Text)
#include "Motorizado.h" Motorizado::Motorizado(string m, int mm, int aaaa, string comb, int cil) :Veiculo(m,aaaa,mm),combustivel(comb),cilindrada(cil) { } Motorizado::~Motorizado() { } void Motorizado::info() const { Veiculo::info(); cout << "Combustivel: " << combustivel << endl; cout << "Cilindrada: " << cilindrada << endl; } float Motorizado::calcImposto() const { if(ano>1995) { if((combustivel=="gasolina"&&cilindrada<=1000)||(combustivel!="gasolina"&&cilindrada<=1500)) return 14,56; if((combustivel=="gasolina"&&cilindrada>1000&&cilindrada<=1300)||(combustivel!="gasolina"&&cilindrada>1500&&cilindrada<=2000)) return 29,06; if((combustivel=="gasolina"&&cilindrada>1300&&cilindrada<=1750)||(combustivel!="gasolina"&&cilindrada>2000&&cilindrada<=3000)) return 45,15; if((combustivel=="gasolina"&&cilindrada>1750&&cilindrada<=2600)||(combustivel!="gasolina"&&cilindrada>3000)) return 113,98; if(combustivel=="gasolina"&&cilindrada>2600&&cilindrada<=3500) return 181,17; if(combustivel=="gasolina"&&cilindrada>3500) return 320,89; } else { if((combustivel=="gasolina"&&cilindrada<=1000)||(combustivel!="gasolina"&&cilindrada<=1500)) return 8,10; if((combustivel=="gasolina"&&cilindrada>1000&&cilindrada<=1300)||(combustivel!="gasolina"&&cilindrada>1500&&cilindrada<=2000)) return 14,56; if((combustivel=="gasolina"&&cilindrada>1300&&cilindrada<=1750)||(combustivel!="gasolina"&&cilindrada>2000&&cilindrada<=3000)) return 22,65; if((combustivel=="gasolina"&&cilindrada>1750&&cilindrada<=2600)||(combustivel!="gasolina"&&cilindrada>3000)) return 54,89; if(combustivel=="gasolina"&&cilindrada>2600&&cilindrada<=3500) return 87,13; if(combustivel=="gasolina"&&cilindrada>3500) return 148,37; } }
Veiculo.h
C++ Syntax (Toggle Plain Text)
#ifndef VEICULO_H_ #define VEICULO_H_ #include <string> using std::string; #include <iostream> using std::cout; using std::endl; class Veiculo { public: Veiculo(string m, int aaaa, int mm); virtual ~Veiculo(); virtual void info() const; bool operator < (const Veiculo &v1) const; virtual float calcImposto() const = 0; private: const string marca; const int mes; protected: const int ano; }; #endif /*VEICULO_H_*/
Veiculo.cpp
C++ Syntax (Toggle Plain Text)
#include "Veiculo.h" Veiculo::Veiculo(string m, int aaaa, int mm):marca(m), ano(aaaa), mes(mm) { } Veiculo::~Veiculo() { } void Veiculo::info() const { cout << "Marca: " << marca << endl; cout << "Ano:" << ano << " Mês:" << mes << endl; } bool Veiculo::operator < (const Veiculo &v1) const { if(ano>v1.ano || (ano==v1.ano && mes>v1.mes)) return true; return false; }
thank you very much in advance and sorry for the disturb, but I think it should not be a big error ;/
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.
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.
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.
•
•
Join Date: Feb 2008
Posts: 25
Reputation:
Solved Threads: 0
•
•
•
•
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.
it's working fine now!best regards!
![]() |
Similar Threads
- Class and Sub Class (Java)
- problem with an abstract class (C#)
- Java Class Class problem (Java)
- I have a problem in building class Tree (Binary Search) (Python)
- Problem with objects having objects as attributes (C++)
- unresolved external symbol when using a hashtable class (C++)
- 6 Line class -> Me pounding head into wall (C)
- timing class (Java)
- Derived class problem (C)
Other Threads in the C++ Forum
- Previous Thread: file handling problem
- Next Thread: Problem in homework (new to C++)
Views: 1084 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






