C++ classes, segmentation fault when calling member function

c++classsegmentation-fault

I am now learning C++ and have been experimenting with classes just to get my head around how they work. I have only programmed classes in Java before now.

In my code I have a class definition and a driver to test. In comments I have mention what works and what doesn't. I really want to know why instantiating objects one way works but in other ways I get errors. Is it the compiler, the make file, or the class code? Constructor/default constructor? When I compare my code to others like a textbook, I can't see where I've gone wrong.

Using: code::blocks 10.5 on Linux Mint 13.

Header file:

#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

member function defifnitions:

#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

Driver:

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

Best Solution

What's causing a segfault is the fact that you're flowing off the edge of a function that's supposed to return a string (which is undefined behavior):

string Enemy::display()
//^^^^ you're supposed to return a string
{
    cout<<name<<" -  x: "<<x_pos<<", y: "<<y_pos<<", HP: "<<hitpoints<<endl;
    // no return statement
}

The rest is in @chris' answer.

Related Question