2017-02-10 3 views
-1

Kürzlich Ich begann C++ zu lernen. Ich bin ein Student und ich habe hier einen Workshop. Die Idee ist Konstruktoren im vorhandenen Programm zu entwerfen und zu implementieren. So kann ich nur 2 Dateien von 3 ändern. Ich habe alle Fehler behoben, aber jetzt kompiliert und gibt mir Segmentierung Fehler (Core Dumped). Ich habe keine Ahnung, was ich falsch mache. Ich kann nur passenger.h und passenger.cpp ändern. Kann jemand bitte den Code ansehen? Wo ist der Fehler passiert? Wie gehe ich damit um?C++ Konstruktoren Segmentierungsfehler (core dumped)

//passenger.h 
//TODO: add header guards here 
#ifndef _PASSENGER_H_ 
#define _PASSENGER_H_ 
// TODO: holiday namespace here 
namespace holiday{ 
    // TODO: declare the class Passenger here 
    class Passenger { 
     // TODO: add the class the attributes, 
     char m_name[32]; 
     char m_destination[32]; 
     int m_departureYear; 
     int m_departureMonth; 
     int m_departureDay; 
    public:  
     //  the member function already provided, 
     void display(bool onlyNames = false) const; 
     //  and the member functions that you must implement 
     Passenger(); 
     Passenger(const char pName[],const char pDestination[]); 
     bool isEmpty() const; 
     bool canTravelWith(const Passenger&) const; 
    }; 
} 
#endif 

-

//passenger.cpp 
// TODO: add your headers here 
#include "passenger.h" 
#include <iostream> 
#include <cstring> 
// TODO: add the namespaces that you will be using here 
using namespace std; 
// TODO: holiday namespace here 
namespace holiday{ 
    // TODO: add the default constructor here 
    Passenger::Passenger(){ 
     m_name[0] = '\0'; 
     m_destination[0] = '\0'; 
     m_departureYear = 0; 
     m_departureMonth = 0; 
     m_departureDay = 0; 
    } 
    // TODO: add the constructor with 2 parameters here 
    Passenger::Passenger(const char pName[],const char pDestination[]){ 
     //safe state 
     m_name[0] = '\0'; 
     m_destination[0]= '\0'; 
     m_departureYear = 0; 
     m_departureMonth = 0; 
     m_departureDay = 0; 
     //validation 
     if (pName[0] != 0 && pName != nullptr && 
      pDestination[0] != 0 && pDestination != nullptr){ 
       strcpy(m_name,pName); 
       strcpy(m_destination,pDestination); 
       m_departureYear = 2017;        
       m_departureMonth = 7; 
       m_departureDay = 1; 
     } 
    } 
    // TODO: add the canTravelWith(...) function here 
    bool Passenger::canTravelWith(const Passenger& pPassenger) const{ 
     bool _together; 
     if(m_departureYear == pPassenger.m_departureYear && 
      m_departureMonth == pPassenger.m_departureMonth && 
      m_departureDay == pPassenger.m_departureDay && 
      strcmp(m_destination, pPassenger.m_destination) == 0) {  
       _together = true;   
      } else { 
       _together = false; 
      } 
      return _together; 
    } 
    // TODO: add the isEmpty() function here 
    bool Passenger::isEmpty() const{ 
     bool _empty; 
     if (m_name[0] == '\0' && 
     m_destination[0] == '\0' && 
     m_departureYear == 0 && 
     m_departureMonth == 0 && 
     m_departureDay == 0){ 
      _empty = true; 
     } else { 
      _empty = false; 
     } 
     return _empty; 
    } 
    // below is the member function already provided 
    // TODO: read it and understand how it accomplishes its task 
    void Passenger::display(bool nameOnly) const 
    { 
     if (false == this->isEmpty()) 
     { 
      cout << this->m_name; 
      if (false == nameOnly) 
      { 
       cout << " will travel to " << this->m_destination << ". " 
        << "The journey will start on " 
        << this->m_departureYear << "-" 
        << this->m_departureMonth << "-" 
        << this->m_departureDay 
        << "." << endl; 
      } 
     } 
     else 
     { 
      cout << "Invalid passenger!" << endl; 
     } 
    } 

}//holiday 

-

//main.cpp 
#include <iostream> 
#include "passenger.h" 
#include "passenger.h" // this is intentional 

using namespace std; 
using namespace holiday; 

int main() 
{ 
    Passenger travellers[] = { 
     Passenger(nullptr, "Toronto"), 
     Passenger("", "Toronto"), 
     Passenger("John Smith", nullptr), 
     Passenger("John Smith", ""), 
     Passenger("John Smith", "Toronto"), // valid 
     Passenger(nullptr, nullptr), 
     Passenger() 
    }; 
    cout << "----------------------------------------" << endl; 
    cout << "Testing the validation logic" << endl; 
    cout << "(only passenger 5 should be valid)" << endl; 
    cout << "----------------------------------------" << endl; 
    for (unsigned int i = 0; i < 7; ++i) 
    { 
     cout << "Passenger " << i + 1 << ": " << (travellers[i].isEmpty() ? "not valid" : "valid") << endl; 
    } 
    cout << "----------------------------------------" << endl << endl; 

    Passenger vanessa("Vanessa", "Paris"), 
       mike("Mike", "Tokyo"), 
       alice("Alice", "Paris"); 

    cout << "----------------------------------------" << endl; 
    cout << "Testing the display function" << endl; 
    cout << "----------------------------------------" << endl; 
    vanessa.display(); 
    mike.display(); 
    alice.display(); 
    cout << "----------------------------------------" << endl << endl; 

    cout << "----------------------------------------" << endl; 
    cout << "Testing the travelling together logic" << endl; 
    cout << "----------------------------------------" << endl; 
    cout << "Can Vanessa and Mike travel together (should be NO)? " 
     << (vanessa.canTravelWith(mike) ? "YES" : "NO") << endl; 
    cout << "Can Vanessa and Alice travel together (should be YES)? " 
     << (vanessa.canTravelWith(alice) ? "YES" : "NO") << endl; 
    cout << "Can Alice and Vanessa travel together (should be YES)? " 
     << (alice.canTravelWith(vanessa) ? "YES" : "YES") << endl; 
    cout << "Can Mike and Alice travel together (should be NO)? " 
     << (mike.canTravelWith(alice) ? "YES" : "NO") << endl; 
    cout << "----------------------------------------" << endl << endl; 


    return 0; 
} 
+1

Bitte beachten Sie, dass C und C++ verschiedene Sprachen sind. Bitte verwenden Sie nur das relevante Sprach-Tag. – kaylum

+2

Dies ist nicht das Problem, aber Namen, die mit einem Unterstrich beginnen, gefolgt von einem Großbuchstaben ('_PASSENGER_H_') und Namen, die zwei aufeinander folgende Unterstriche enthalten, sind für die Implementierung reserviert. Benutze sie nicht in deinem Code. –

Antwort

1

In Ihrem Code Sie haben:

if (pName[0] != 0 && pName != nullptr && 
     pDestination[0] != 0 && pDestination != nullptr){ 

Beachten Sie, dass der Anrufer shrewdly (absichtlich) vorbeiführt null pName und pDestination.

Kurzschlüsse Lesen Sie mehr über Zustand und herauszufinden, ob Sie die Reihenfolge, in der diese Kontrollen durchführen ändern müssen.


getrennt, aber genauso wichtig ist, könnten Sie in member initialization lists suchen.

+0

Alles macht jetzt Sinn, danke für die Info! Regel # 1 überprüfe immer nullptr! –

0

Die Tests wie pName[0] != 0 && pName != nullptr sind in der falschen Reihenfolge. Sie müssen zuerst überprüfen, ob pName null ist. Der Aufrufer übergibt nullptr, sodass der Zugriff auf vor der Überprüfung, dass pName nicht null ist, zu undefiniertem Verhalten führt.

+0

Ja, das hat geklappt! Vielen Dank! –

Verwandte Themen