简体   繁体   中英

Unable to find segmentation fault error in c++

I have 3 c++ files, instrument.h, percussion.h, and instrumentApp.cpp . Instrument.h is the base class and percussion.h inherits it. Percussion objects are defined and implemented in the instrumentApp.cpp class. Whenever I run instrumentApp.cpp , I get the segmentation fault error.

I have managed to trace the cause of the error to the overloaded << operator function in percussion.h where I am calling a method of the base class instrument.h . For some reason, my code is unable to call methods of the base class and I don't know why. Can you please help me?

Here is the instrument.h class

#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

Here is the percussion.h class

#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

Here is the implementation file 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;
}

The problem here is that I wasn't returning the os object when I overloaded the << operator.

The fix is as follows in the percussion.h file

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

        os<<percussion.getName();

        return os;

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM