C++类,调用成员函数时出现段错误

标签 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

成员函数定义:

#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

司机:

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

最佳答案

导致段错误的原因是您超出了应该返回 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
}

剩下的在@chris 的回答中。

关于C++类,调用成员函数时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16347034/

相关文章:

C++ 类 : read value behave as different types

python - 如何在Python中捕获SegFault异常?

c++ - POCO C++ 中的 SSL 异常

c++ - QT QSqlTableModel - 给定数据列中的背景颜色

C++:如何将 'PCZZWSTR' 类型更改为 'char',反之亦然?

PHP 自动加载 - 找到文件 - 类 fatal error

c++ - 如何将openCV项目构建为dll

c++ - 类构造函数、重载构造函数和对象初始化

c++ - 在没有测试数据的情况下查找段错误

c - mmap 问题 -> 段错误