【发布时间】: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