2016-11-06 5 views
0

nicht finden Ich habe 3 C++ Dateien, instrument.h, percussion.h, and instrumentApp.cpp. Instrument.h ist die Basisklasse und percussion.h erbt es. Percussion Objekte sind in der Klasse instrumentApp.cpp definiert und implementiert. Immer wenn ich instrumentApp.cpp starte, erhalte ich den Segmentierungsfehlerfehler.Kann Segmentierungsfehler Fehler in C++

Ich habe es geschafft, die Ursache des Fehlers auf die überladene << operator Funktion in percussion.h zu verfolgen, wo ich eine Methode der Basisklasse instrument.h aufrufen. Aus irgendeinem Grund kann mein Code Methoden der Basisklasse nicht aufrufen und ich weiß nicht warum. Kannst du mir bitte helfen? Hier

ist die instrument.h Klasse

#ifndef INSTRUMENT_H 
#define INSTRUMENT_H 


class Instrument{ 
     private: 
       std::string name; 
       std::string sound; 
       std::string lowRange; 
       std::string highRange; 
     public: 
       Instrument(std::string name, std::string sound, std::string lowRange, std::string highRange){ 
         this->name = name; 
         this->sound = sound; 
         this->lowRange = lowRange; 
         this->highRange = highRange; 
       } 

       std::string getName() const{ 
         return this->name; 
       } 

       std::string play()const { 
         return this->sound; 
       } 

       std::string getLowRange() const{ 
         return this->lowRange; 
       } 

       std::string getHighRange() const{ 
         return this->highRange; 
       } 

       bool isWind(); 
       bool isWoodWind(); 
       bool isBrass(); 
       bool isKeyboard(); 
       bool isPercussion(); 
       bool isStrings(); 

       friend std::ostream &operator <<(std::ostream &os, const Instrument &instrument){ 
       } 
}; 

#endif 

Hier wird die percussion.h Klasse

#ifndef PERCUSSION_H 
#define PERCUSSION_H 

#include "instrument.h" 

class Percussion : public Instrument{ 
     private: 
       bool struck; 
     public: 
       Percussion(std::string name, std::string sound, std::string lowRange, std::string highRange, bool struck) : Instrument(name,sound,lowRange,highRange){ 
         this->struck=struck; 

       } 

       bool isStrucked() const { 
         return this->struck; 
       } 

       bool isPercussion() { 
         return true; 
       } 

       std::string getType() const{ 
         if(this->struck){ 
           return "struck"; 
         } 
         else{ 
           return ""; 
         } 
       } 

       friend std::ostream &operator <<(std::ostream &os, Percussion &percussion){ 
      //The error stems from this line of code 
      //Apparently, the getName() method in the base class isn't called 

          os<<percussion.getName(); 

       } 

}; 

#endif 

Hier wird die Implementierungsdatei instrumentApp.cpp

#include <iostream> 
#include <string> 
#include <sstream> 
#include <cstdlib> 

#include "instrument.h" 

#include "percussion.h" 
#include "strings.h" 

using namespace std; 



int main() { 

    Percussion timpani("timpani", "boom", "D2", "A2", true); 
    cout << timpani << endl; 

    Percussion harp("harp", "pling", "Cb1", "F#7", false); 
    cout << harp << endl; 

    return 0; 
} 
+1

Ich bin überrascht, dies stellt auch da Sie nichts von Ihrem 'Operator zurückkehren <<' in jeder Klasse – pat

+2

... und das ist genau das Problem. –

+0

Ich mache in der Klasse percussion.h – Avi

Antwort

1

Das Problem hier ist, dass Ich gab das OS-Objekt nicht zurück, als ich die <überlasteteBetreiber.

Das Update ist wie folgt in der Datei percussion.h

friend std::ostream &operator <<(std::ostream &os, Percussion &percussion){ 

     os<<percussion.getName(); 

     return os; 

} 
+0

Der Percussion-Parameter sollte auch eine konstante Referenz sein, nicht nur eine Referenz. – PaulMcKenzie