简体   繁体   English

在C ++中找不到分段错误错误

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

I have 3 c++ files, instrument.h, percussion.h, and instrumentApp.cpp . 我有3个c ++文件, instrument.h, percussion.h, and instrumentApp.cpp Instrument.h is the base class and percussion.h inherits it. Instrument.h是基类,而percussion.h继承了它。 Percussion objects are defined and implemented in the instrumentApp.cpp class. Percussion对象在instrumentApp.cpp类中定义和实现。 Whenever I run instrumentApp.cpp , I get the segmentation fault error. 每当我运行instrumentApp.cpp ,都会出现分段错误错误。

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 . 我设法将错误的原因追溯到percussion.h中重载的<< operator函数,在这里我正在调用基类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 这是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

Here is the percussion.h class 这是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

Here is the implementation file instrumentApp.cpp 这是实现文件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. 这里的问题是,当我重载<<操作符时,我没有返回os对象。

The fix is as follows in the percussion.h file 该修复程序在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