对象向量的抽象函数调用中的分段错误

时间:2019-05-24 00:05:16

标签: c++ abstract-class virtual-functions

我正在尝试调用类对象的虚函数。该对象位于2d向量中,我确定它存在,因为它会打印出我分配给它的“符号”。我有一个抽象基类,我的其他类都派生自该基类。但是,每当我尝试调用此函数时,都会出现段错误。

// In my Cave class constructor, I create my 2d array, and make all the elements default constructors of a room which is an 'Empty,' room.
Room r;
                grid = vector<vector<Room> > (width, vector<Room>(width));
                for(int i = 0; i < width; i ++){
                        for(int j = 0; j < width; j++){
                                Room r;
                                grid[i][j] = r;
                        }
                }
//Here is where I set up all of my 'special,' Rooms
Room g('G');
                        Room b('B');
                        Room b2('B');
                        Room p1('P');
                        Room p2('P');
                        Room w('W');

                        this->grid[0][0] = g;
                        this->grid[0][1] = b;
                        this->grid[0][2] = b2;
                        this->grid[0][3] = p1;
                        this->grid[0][4] = p2;
                        this->grid[0][5] = w;
// This is my room class .cpp I have a non default constructor that should make a new Event object of one of the other object
#include <iostream>
#include <vector>
#include <cstdlib>
#include "room.h"
#include "event.h"
#include "gold.h"
#include "wumpus.h"
#include "pit.h"
#include "empty.h"
#include "bats.h"

using namespace std;

        Room::Room(){
                cout << "making room" << endl;
                symbol = ' ';
                Event *e = new Empty;
        }
        Room::Room(char c){
                cout << "Non-defualt" << endl;
                if(c == 'G'){
                        Event *e = new Gold;
                        symbol = 'G';
                }
                else if(c == 'W'){
                        Event *e = new Wumpus;
                        symbol = 'W';
                }
                else if(c == 'P'){
                        Event *e = new Pit;
                        symbol = 'P';
                }
                else if(c == 'B'){
                        Event *e = new Bats;
                        symbol = 'B';
                }

        }
        void Room::sense(){
                cout << this->symbol << endl;
                this->e->percept();
        }
        char Room::get_symbol(){
                return(this->symbol);
        }
// my Room header file
#ifndef ROOM_H
#define ROOM_H

#include <iostream>
#include "event.h"
#include "bats.h"
#include "empty.h"
#include "wumpus.h"
#include "gold.h"
#include "pit.h"

using namespace std;

class Room {
        public:
                Event *e;
                char symbol;
                Room();
                Room(char);
                void sense();
};
#endif
//here is where I call my 'Sense()' function that is apart of my room class
void Cave::nearby(){
                int x = p.spot/width;
                this->grid[(x - 1)][p.spot % width].sense();
                this->grid[(x + 1)][p.spot % width].sense();
                this->grid[x][(p.spot % width) + 1].sense();
                this->grid[x][(p.spot % width) - 1].sense();
        }
// my abstract event header file
#ifndef EVENT_H
#define EVENT_H
#include <iostream>
using namespace std;
class Event {
        public:
                Event();
                virtual void percept() = 0;
                virtual void encounter() = 0;
};
#endif
//my abstract event.cpp
#include <iostream>
#include "event.h"

using namespace std;

        Event::Event(){
                cout << "making event" << endl;
        }
//Finally my header of the different room types, this one is gold. but they are all the exact same outline and stuff
#ifndef GOLD_H
#define GOLD_H

#include <iostream>
#include <string>
#include "event.h"

using namespace std;

class Gold : public Event {
        private:
                string name;
        public:
                Gold();
                void percept();
                void encounter();
};
#endif
//here is my gold.cpp
#include <iostream>
#include <string>
#include "gold.h"
#include "event.h"

using namespace std;

        Gold::Gold(){
                name = "gold";
                cout << "making gold" << endl;
        }
        void Gold::percept(){
                cout << "You see a glimmer nearby..." << endl;
        }
        void Gold::encounter(){
                cout << "encounter" << endl;
        }

它应该打印出“您看到附近有微光”。或其他任何消息,但我收到了段错误。

1 个答案:

答案 0 :(得分:1)

            if(c == 'G'){
                    Event *e = new Gold;
                    symbol = 'G';
            }

这将创建一个全新的Event *,也称为e,并将其设置为指向new Gold。那不是你想要的。您想要将名为e的现有成员设置为指向new Gold。所以你想要:

            if(c == 'G'){
                    delete e; // don't leak the existing object
                    e = new Gold;
                    symbol = 'G';
            }

您在其他任何地方都有相同的问题,包括此处:

            Event *e = new Empty;

应该是:

            e = new Empty;

如果您的编译器未向您发出有关创建一个新变量的警告,该变量会掩盖现有的类成员,请使用更好的编译器或学习如何调高其警告并一定要注意它们。