Segmantationfault trotz einfacher Datentypen
-
Hallo Leute,
folgende Klassenstruktur besteht:
class Address { private: std::string city; std::string street; std::string zip; std::string country; public: Address(); Address(const std::string& street, const std::string& city, const std::string& zip,const std::string& country); public: // getter und setter }; std::ostream& operator<<(std::ostream& os, const Address& adress);Zuweisungen ueber die Initialisierungsliste des Ctors.
Zur Uebersicht lass ich das mal raus.class Person { private: Address address; public: Person(const Address& birthAdress, char sex); };Zuweisungen hier ueber die Initialisierungsliste des Ctors.
class Student : public Person { private: Address homeAddress; public: Student(const Address& homeAddress, const Address& address); }; Student::Student(const Address& homeAddress,const Address& address) : Person(address,'w') {}Wenn ich eine Person nun ueber
Address s1("asd","asd","asd","dasd"); Address s2(s1); Student s(s1,s2);Krieg ich einen Segmentation Fault an der Stelle wo der Ctor der Person aufgerufen wird und dort der Copy-Ctor der Addresse.
Eigentlich duerfte er doch dort NULL-Probleme haben das zu kopieren oder nicht?
Wenn ich den CopyCtor implementiere, laeuft alles wunderbar.Bitte nicht ueber das Klassendesign diskutieren weil ich habe ne Menge Informationen rausgelassen die nicht von belangen fuer das Problem sind.
Hoffe ihr habt eine Idee.
-
Hast du wirklich nur
std::string-Member? Falls ja, ist vielleicht was kaputt. Neu kompilieren/neues Projekt versuchen. Und sicherstellen, dass es sich um ein Minimalbeispiel handelt, nicht dass du an einem anderen Ort UB erzeugst.Wie sieht denn dein selbstgeschriebener Kopierkonstruktor aus?
-
In der Addresse sind ausschliesslich std::string Attribute.
Ich hab mal zwei Varianten des CopyCtor ausprobiert
Address::Address(const Address& a) : city(a.city), street(a.street), zip(a.zip), country(a.country) { }Der fuehrt auch zu nem Seg-Fault.
[/cpAddress::Address(const Address& a) { this->zip = a.zip; this->country = a.country; this->street = a.street; this->city = a.city; }Dieser nicht.
Ich hab das ganze Projekt mal mit Visual Studio compiliert und ausgefuhert, ohne Copy Ctor, da lief es.
Dann hab ich es noch unter Linux compiliert und ausgefuehrt, auch wieder Seg-Fault
-
Compiler und Version?
Sieht auf jeden Fall nach gültigem C++ aus. Versuch das Ganze mal noch weiter zu reduzieren und den kompletten Code hier zu posten, ggf. müsstest du einen Bug melden...
-
Ok Ich hab es jetzt auf ein Ultimatives Minimal Beispiel runtergebrochen:
#ifndef ADDRESS_H_INCLUDED #define ADDRESS_H_INCLUDED #include <string> #include <sstream> #include <ostream> class Address { private: std::string city; std::string street; std::string zip; std::string country; public: Address(); Address(const std::string& street, const std::string& city, const std::string& zip,const std::string& country); public: const std::string& get_city() const; const std::string& get_street() const; const std::string& get_zip() const; const std::string& get_country() const; void set_city(const std::string& city); void set_street(const std::string& street); void set_zip(const std::string& zip); void set_country(const std::string& country); }; std::ostream& operator<<(std::ostream& os, const Address& adress); #endif // ADDRESS_H_INCLUDED#ifndef PERSON_H_INCLUDED #define PERSON_H_INCLUDED #include <vector> #include "Address.h" #include "Date.h" #include "Name.h" #include "Phone.h" #include "AddressGenerator.h" #include "NameGenerator.h" class Person { private: Address birthAddress; public: Person(const Address& birthAdress); virtual void print_to(std::ostream& os) const = 0; }; std::ostream& operator<<(std::ostream& os, const Person& person); #endif // PERSON_H_INCLUDED #include "Person.h" Person::Person(const Address& birthAdress) : birthAddress(birthAddress) { } void Person::print_to(std::ostream& os) const { os<<"### Personendaten ###"<<std::endl <<"Adresse - "<<this->birthAddress<<std::endl; } std::ostream& operator<<(std::ostream& os, const Person& person) { person.print_to(os); return os; }#ifndef STUDENT_H #define STUDENT_H #include "Person.h" #include <tr1/memory> class Student : public Person { private: Address homeAddress; public: Student(const Address& homeAddress, const Address& address); virtual void print_to(std::ostream& os) const; }; #endif // STUDENT_H #include "Student.h" Student::Student(const Address& homeAddress,const Address& address) : Person(address), homeAddress(homeAddress) {} void Student::print_to(std::ostream& os) const { os<<"#### Studentendaten ####"<<std::endl <<"Heimatanschrift - "<<this->homeAddress<<std::endl; Person::print_to(os); }#include <iostream> #include "Student.h" int main() { Address s1("asd","asd","asd","dasd"); Address s2(s1); Student s(s1,s2); std::cout<<s; }Compiler GCC Version: 4.4.1 glaube ich.
Das ist der Code den ich gerade ausgefuehrt habe und der fuehrt zum Fehler.
-
Was passiert, wenn du die Address Member reduzierst (= nur ein String), die Getter/Setter rausnimmst (ich seh zumindest nicht, wo die verwendet werden) usw.? Der komplette Code ist das auch nicht, die ganzen Header aus Person.h finde ich hier nicht, Address.cpp fehlt auch.
Minimal compilierbares Beispiel = ALLER Code, aber nacheinander alles rausnehmen, was nicht verantwortlich scheint.
-
Boah wie daemlich muss man sein.
Person::Person(const Address& birthAdress) : birthAddress(birthAddress) { }Fehler gefunden: "birthAddress(birthAddress)"
Der Parameter heisst aber "birthAdress"
-
autsch

Reduktion aufs Wesentliche hat demnach geholfen?
-
in c++ hängt man gewöhnlich bei membervariablen einen unterstrich dran, dann fällt das auch schneller auf
-
_ schrieb:
in c++ hängt man gewöhnlich bei membervariablen einen unterstrich dran
Nö, das ist Konventionssache. Manche machen es so, (viele) andere machen es nicht.
Mich wundert nur, dass keiner der Compiler da wenigstens ne Warnung rausgehauen hat, dass ein Member mit sich selbst initialisiert werden sollte.
-
pumuckl schrieb:
Mich wundert nur, dass keiner der Compiler da wenigstens ne Warnung rausgehauen hat, dass ein Member mit sich selbst initialisiert werden sollte.
Es wird wahrcheinlich mal wieder ohne Warnungen gearbeitet. Eine Warnung zu nicht benutzten Funktionsparametern kommt evtl. auch noch.
-
Ja Reduktion hat geholfen

Wie peinlich man
