簡體   English   中英

在C ++中找不到分段錯誤錯誤

[英]Unable to find segmentation fault error in c++

我有3個c ++文件, instrument.h, percussion.h, and instrumentApp.cpp Instrument.h是基類,而percussion.h繼承了它。 Percussion對象在instrumentApp.cpp類中定義和實現。 每當我運行instrumentApp.cpp ,都會出現分段錯誤錯誤。

我設法將錯誤的原因追溯到percussion.h中重載的<< operator函數,在這里我正在調用基類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;
}

這里的問題是,當我重載<<操作符時,我沒有返回os對象。

該修復程序在percussion.h文件中如下所示

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

        os<<percussion.getName();

        return os;

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM