Mat*_*hop 3 c++ class segmentation-fault
我现在正在学习C++并且一直在试验课程,以便了解它们的工作方式.我之前只用Java编写过类.
在我的代码中,我有一个类定义和一个要测试的驱动程序.在评论中我提到了什么有效,什么没有.我真的想知道为什么以一种方式实例化对象,但在其他方面我得到错误.它是编译器,make文件还是类代码?构造函数/默认构造函数?当我将我的代码与教科书中的其他代码进行比较时,我无法看到我出错的地方.
在Linux Mint 13上使用:code :: blocks 10.5.
头文件:
#ifndef ENEMY_H
#define ENEMY_H
#include <string>
using namespace std;
class Enemy
{
public:
Enemy();
Enemy(int, int, string);
~Enemy();
string display();
// setter and getters:
int getHP();
void setHP(int);
int getX();
void setX(int);
int getY();
void setY(int);
string getName();
void setName(string);
private:
int hitpoints;
int x_pos;
int y_pos;
string name;
};
#endif // ENEMY_H
Run Code Online (Sandbox Code Playgroud)
成员函数定义:
#include "Enemy.h"
#include <iostream>
#include <string>
// default ctor
Enemy::Enemy()
{
cout << "Creating enemy with default ctor.\n";
}
//ctor
Enemy::Enemy(int x, int y, string str)
{
cout << "Creating object with name: " << str << endl;
setX(x);
setY(y);
setHP(100);
setName(str);
}
//dtor
Enemy::~Enemy()
{
cout << "destroying Enemy: " << name << endl;
}
string Enemy::display()
{
cout<<name<<" - x: "<<x_pos<<", y: "<<y_pos<<", HP: "<<hitpoints<<endl;
}
int Enemy::getHP(){
return hitpoints;
}
void Enemy::setHP(int hp){
hitpoints = hp;
}
int Enemy::getX(){
return x_pos;
}
void Enemy::setX(int x){
x_pos = x;
}
int Enemy::getY(){
return y_pos;
}
void Enemy::setY(int y){
y_pos = y;
}
string Enemy::getName(){
return name;
}
void Enemy::setName(string objectName){
name = objectName;
}
// end of function definitions
Run Code Online (Sandbox Code Playgroud)
司机:
#include "Enemy.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Program started.\n" << endl;
// initialise a few Enemy objects
Enemy E1(1, 1, "E1");
Enemy E2(2, -4, "E2");
Enemy E3;
Enemy E4;
Enemy *E5 = new Enemy(4, 5, "E5");
E1.display(); // <- success!
E2.display(); // <- success!
E3.display(); // <- segmentation fault at run time
E4.display(); // <- segmentation fault at run time
E5.display(); // <- compile time error "request for member
// 'display'" in 'E5'. which is of
// non-class type 'enemy'
cout << "end of program.\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当你说Enemy E3;,你调用默认构造函数.它负责默认初始化您的数据成员.虽然这意味着您的std::string成员被初始化为空字符串,但这也意味着您的其他成员未被初始化(就像做int i; std::cout << i;坏事一样).当您在输出语句中读取值时,这会导致未定义的行为,这意味着任何事情都可能发生.它选择了崩溃.
至于E5,它是一个指针.您需要取消引用它以获取可以调用成员的对象:
(*E5).display();
Run Code Online (Sandbox Code Playgroud)
还有一个快捷方式可以做到这一点:
E5->display();
Run Code Online (Sandbox Code Playgroud)
正如jrok指出的那样,你也不会从中返回任何东西display,即使它应该返回一个std::string.这是除了每个函数的未定义行为main,它将在到达右括号时返回0.
作为旁注,你有内存泄漏,因为你没有delete E5;.在这种情况下,几乎可以肯定操作系统将负责释放内存,但将其放入循环中,您将看到内存上升和上升.
如果需要动态分配单个对象,请使用智能指针:
std::unique_ptr<Enemy> E5(new Enemy(4, 5, "E5"));
Run Code Online (Sandbox Code Playgroud)
在C++ 14中,std::make_unique幸运的是,我们也正在消除对它的需求new.
造成段错误的原因是你正在流出一个应该返回一个函数的边缘string(这是一个未定义的行为):
string Enemy::display()
//^^^^ you're supposed to return a string
{
cout<<name<<" - x: "<<x_pos<<", y: "<<y_pos<<", HP: "<<hitpoints<<endl;
// no return statement
}
Run Code Online (Sandbox Code Playgroud)
剩下的就是@chris的回答.