Marcus-Bäckmann_OOP-Für-Dummies Kap07 address4.cpp address4.h load4.cpp fehler beim compilieren
-
hallo
ich wollte dieses beispiel compilieren das funktioniert aber nicht
ich habe es mit dem gcc(linux) und dev+vc++(win) getestet jedesmal
kommen fehler über eine virtuelle funktion(looser, override)
woran liegt das ich habe über die suche nichts gefunden und die seite zum buch scheint es auch nicht mehr zu geben
vielen dank für eure hilfe
-
Hi,
Warum postest du nicht einfach mal die Source?
-
address4.h:
#ifndef _ADDRESS_H #define _ADDRESS_H #include <string> #include <exception> class AddressException : public std::exception { public: AddressException(const std::string& errortext) { m_Errortext = errortext; } virtual const char* what() const { return m_Errortext.c_str(); } private: std::string m_Errortext; }; class Address { public: Address(const std::string& filename) throw(AddressException); std::string getName1() const { return m_Name1; } std::string getName2() const { return m_Name2; } std::string getStreet() const { return m_Street; } std::string getTown() const { return m_Town; } private: std::string m_Name1; std::string m_Name2; std::string m_Street; std::string m_Town; }; #endifaddress4.cpp:
#include "address4.h" #include <string> #include <fstream> using namespace std; Address::Address(const string& filename) { ifstream thefile(filename.c_str()); if (thefile) { getline(thefile, m_Name1); if (!thefile.good()) throw AddressException("Fehler im Feld Name 1"); getline(thefile, m_Name2); if (!thefile.good()) throw AddressException("Fehler im Feld Name 2"); getline(thefile, m_Street); if (!thefile.good()) throw AddressException("Fehler im Feld Strasse"); getline(thefile, m_Town); if (thefile.bad() || thefile.fail()) throw AddressException("Fehler im Feld Ort"); } else throw AddressException("Datei nicht geoeffnet"); }load4.cpp
#include "address4.h" #include <iostream> using namespace std; int main() { try { Address theAddress("datafile2.txt"); cout << theAddress.getName1() << endl; cout << theAddress.getName2() << endl; cout << theAddress.getStreet() << endl; cout << theAddress.getTown() << endl; } catch (AddressException& e) { cout << "Adressdaten nicht geladen!" << endl; cout << "Fehlergrund: " << e.what() << endl; } return 0; }gcc-aufruf:
g++ load.cpp address.cpp -o address4address5+6+7 funktioniert auch nicht (der selbe fehler)
-
Mit ein paar änderungen hab ichs ohne fehler compiliert.
in address4.h destructor declariertvirtual ~AddressException() throw() {}in address4.cpp throw(AddressException) dazugefügt
Address::Address(const string& filename)throw(AddressException)Kurt
-
Hallo,
alternativ kannst du AddressException auch einfach von std::runtime_error ableiten. Diese Klasse hat keine so strengen Exception-Spezifikationen.