Программа печатает на экран то что нужно, но в конце вылетает с ошибкой. Посмотрите пожалуйста, что не так... Ошибку делает в 29 строке UnterElement& rue(dynamic_cast<UnterElement&>(c1[i]));Это пример от препода, ну тоисть весь мейн: | Код | c1 << c2 << c2; // c1 enthaelt nun Element- und UnterElement-Objekte for (int i=0; i<c1; i++) { try { UnterElement& rue(dynamic_cast<UnterElement&>(c1[i])); cout << "UnterElement-Objekt:" << rue << endl; } catch (bad_cast) { cout << "Element-Objekt:" << static_cast<const string>(c1[i]) << endl; } }
|
Или все правильно работает, и должно выдавать cout в блоке catch???? Запутался.... :(Вот исходники. main | Код | #include <iostream> using namespace std; #include "Container.h" #include "Element.h"
int main() { Container c1; Container c2;
// Container c1 fuellen und ausgeben c1 << Element("Eins")<< Element("Zwei") << Element("Drei"); for (int i=0; i<c1; i++) { cout << static_cast<const string>(c1[i]) << " "; } cout << endl;
// Container c2 fuellen und ausgeben c2 << UnterElement("One")<< UnterElement("Two") << UnterElement("Three"); for (int i=0; i<c2; i++) { cout << dynamic_cast<UnterElement&>(c2[i]) << " "; } cout << endl;
// Container c2 zweimal in c1 kopieren und c1 ausgeben c1 << c2 << c2; // c1 enthaelt nun Element- und UnterElement-Objekte for (int i=0; i<c1; i++) { try { UnterElement& rue(dynamic_cast<UnterElement&>(c1[i])); cout << "UnterElement-Objekt:" << rue << endl; } catch (bad_cast) { cout << "Element-Objekt:" << static_cast<const string>(c1[i]) << endl; } } return 0; }
|
container.cpp | Код | #include "Container.h" #include "Element.h"
using namespace std; //-----------------------------------------------------------------------------
Container::Container(const Container& r) {
Anzahl = r.Anzahl; Groesse = r.Groesse; expand(100);
for (int i = 0; i < Anzahl; ++i) t[i] = r.t[i];
}
//-----------------------------------------------------------------------------
void Container::expand(int s) {
Element **array;
Groesse += s;
try { array = new Element*[Groesse]; } catch (bad_alloc) { cerr << "Fehler bei Speicher reservierung" << endl; throw 1; }
for (int i = 0; i < Groesse; ++i) array[i] = NULL;
if (t != NULL) { for (int i = 0; i < Anzahl; ++i) array[i] = t[i]; }
delete []t; t = array; }
//-----------------------------------------------------------------------------
Container& Container::operator =(const Container& r) { delete []t;
Anzahl = r.Anzahl; Groesse = r.Groesse; expand(100);
for (int i = 0; i < Anzahl; ++i) t[i] = r.t[i]; }
//-----------------------------------------------------------------------------
Container::operator int() const {
return Anzahl; } //-----------------------------------------------------------------------------
Container& Container::operator <<(const Element& v) { t[Anzahl] = v.clone(); Anzahl++; return *this; } //-----------------------------------------------------------------------------
Container& Container::operator <<(const Container& r) {
Anzahl += r.Anzahl; if (Anzahl > Groesse) expand(r.Groesse); for (int i = 0; i < r.Anzahl; ++i) t[Anzahl + i] = r.t[i]; return *this; } //-----------------------------------------------------------------------------
Element& Container::operator [](int i) { if (i >= 0 && i < Groesse) return *t[i]; else throw 1; } //-----------------------------------------------------------------------------
Container::~Container() { delete []t; };
|
container.h | Код | #ifndef _Container_h_ #define _Container_h_ #include <iostream> #include <iomanip>
using namespace std;
class Element;
class Container { Element** t; int Anzahl; int Groesse; Container& operator= (const Container&); // Zuweisungsoperator sperren Container(const Container&); // Kopierkonstruktor sperren // Eigene private Attribute und Methoden public: Container():t(NULL),Anzahl(0),Groesse(0){expand(100);}; virtual ~Container(); operator int() const; Container& operator<< (const Element &v); Container& operator<< (const Container& r); Element& operator[] (int i); void expand(int); };
#endif
|
Element.h | Код | #ifndef _Element_h_ #define _Element_h_
#include <iomanip> #include <iostream> #include <string>
using namespace std;
class Element { protected: string Zeichenkette; // Die Klasse kann eine Zeichenkette speichern public: Element(const Element& r): Zeichenkette(r.Zeichenkette){} // Kopierkonstruktor Element(string s): Zeichenkette(s) {}; // Konstruktor speichert die uebergebene Zeichenkette virtual ~Element() {}; virtual Element* clone() const { return new Element(*this); } // clone-Methode, erzeugt eine Kopie des aktuellen Objekts operator const string&() const { return Zeichenkette; } // Referenz auf die Zeichenkette nach aussen geben };
class UnterElement : public Element { public: UnterElement(const UnterElement& r): Element(r){} // Kopierkonstruktor UnterElement(string s): Element(s) {}; // Konstruktor ruft Basis-Konstruktor auf virtual Element* clone() const { return new UnterElement(*this); } // clone-Methode, erzeugt eine Kopie des aktuellen Objekts operator const char*() const { return Zeichenkette.c_str(); } // Zeiger auf const char*-Zeichenkette nach aussen geben };
#endif
|
Спасибо. Это сообщение отредактировал(а) Killer_13 - 30.11.2010, 20:06
|