对象向量抽象函数调用的分段错误
Segmentation fault on abstract function call of vector of objects
我正在尝试调用 class 对象的虚函数。该对象位于二维向量中,我确定它存在,因为它打印出我分配给它的 'symbol,' 。我有一个抽象基础 class,我的其他 classes 派生自该基础。但是,每当我尝试调用此函数时,它都会给我一个段错误。
// 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;
}
它应该打印出 "you see a glimmer nearby." 或任何其他消息,但我得到了段错误。
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;
如果您的编译器没有警告您创建一个隐藏现有 class 成员的新变量,请获得更好的编译器或学习如何打开它的警告并确保注意他们。
我正在尝试调用 class 对象的虚函数。该对象位于二维向量中,我确定它存在,因为它打印出我分配给它的 'symbol,' 。我有一个抽象基础 class,我的其他 classes 派生自该基础。但是,每当我尝试调用此函数时,它都会给我一个段错误。
// 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;
}
它应该打印出 "you see a glimmer nearby." 或任何其他消息,但我得到了段错误。
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;
如果您的编译器没有警告您创建一个隐藏现有 class 成员的新变量,请获得更好的编译器或学习如何打开它的警告并确保注意他们。