【问题标题】:Segmentation fault on abstract function call of vector of objects对象向量抽象函数调用的分段错误
【发布时间】:2019-05-24 00:05:16
【问题描述】:

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

// 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;
        }

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

【问题讨论】:

  • 与您的问题无关,但您的第一个 sn-p 中的循环是多余的;矢量对象已经使用它们的默认构造函数构造了
  • 那是一大堆代码。先做一些调试,更准确地找出发生了什么并在这里分享。
  • 使用minimal reproducible example,因为代码太多而且不完整。缩小问题范围通常会导致您自己找到解决方案。
  • 顺便说一句,在第三个代码块(Room 构造函数)中,您没有初始化 成员变量 e,而只是声明了一个 local变量(恰好同名)。
  • @goodvibration 我同意这是很多代码,老实说我不知道​​问题出在哪里,所以我想我可能需要发布比平时更多的代码。

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


【解决方案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;

如果您的编译器没有警告您创建新变量以影响现有类成员,请使用更好的编译器或学习如何打开其警告并确保注意它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    相关资源
    最近更新 更多