无法在c ++中找到分段错误错误

时间:2016-11-06 02:43:11

标签: c++ segmentation-fault

我有3个c ++文件,instrument.h, percussion.h, and instrumentApp.cppInstrument.h是基类,percussion.h继承它。 Percussion类中定义并实现了instrumentApp.cpp个对象。每当我运行instrumentApp.cpp时,我都会收到分段错误错误。

我已设法将错误原因跟踪到<< operator中重载的percussion.h函数,我在其中调用基类instrument.h的方法。出于某种原因,我的代码无法调用基类的方法,我不知道为什么。你能帮我吗?

这是instrument.h类

#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

这是percussion.h类

#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

以下是实施文件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 个答案:

答案 0 :(得分:1)

这里的问题是当我重载&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;操作

percussion.h文件

中的修复方法如下
friend std::ostream &operator <<(std::ostream &os,  Percussion &percussion){

        os<<percussion.getName();

        return os;

 }