我现在正在学习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 …Run Code Online (Sandbox Code Playgroud)