我星期六晚上没有为万圣节打扮,而是试图学习CPP:D
无论如何有人可以帮助我,下面我已经包含了我的源代码,基本上当我尝试编译这个表单时终端我遇到了很多错误,基本上说明变量"name,ho,etc"没有声明,但是我已经包含了我的头文件,所以有人请求看看这个并且可能告诉我缺少什么?非常感谢你们!
#ifndef __TPLAYER__
#define __TPLAYER__ //prevent multiple #includes
TPlayer
{
private:
char name;
int hp;
int dmg;
int wep;
public:
TPlayer(void);
~TPlayer(void);
//Naming
void SetName(char *_name);
char GetName(void);
//Health
void SetHealth(int *_hp);
int GetHealth(void);
//Damage
int SetDamage(int *_dmp)
//Weapon
void SetWeapon(int *_wep);
int GetWeapon(void);
};
#endif /* TPlayer.h */
Run Code Online (Sandbox Code Playgroud)
这是我的源文件:
#include "TPlayer.h"
/////////////////
// Constructor
/////////////////
TPlayer::TPlayer(void)
{
name = "";
hp = 0;
dmg = 0;
wep = 0;
}
///////////////////
// Destructor
///////////////////
~TPlayer::TPlayer()
{
delete name;
delete hp;
delete dmg;
delete wep;
}
///////////////////
// Naming
///////////////////
void SetName(char *_name)
{
name = _name;
}
char GetName(void)
{
return *name;
}
Run Code Online (Sandbox Code Playgroud)
等等,但它告诉我,例如,名称等没有如下所示:
TPlayer.h:4: error: function definition does not declare parameters
TPlayer.cpp:6: error: ‘TPlayer’ has not been declared
TPlayer.cpp:6: error: ISO C++ forbids declaration of ‘TPlayer’ with no type
TPlayer.cpp: In function ‘int TPlayer()’:
TPlayer.cpp:8: error: ‘name’ was not declared in this scope
TPlayer.cpp:9: error: ‘hp’ was not declared in this scope
TPlayer.cpp:10: error: ‘dmg’ was not declared in this scope
TPlayer.cpp:11: error: ‘wep’ was not declared in this scope
TPlayer.cpp: At global scope:
TPlayer.cpp:16: error: expected class-name before ‘::’ token
TPlayer.cpp: In function ‘void SetName(char*)’:
TPlayer.cpp:30: error: ‘name’ was not declared in this scope
TPlayer.cpp: In function ‘char GetName()’:
Run Code Online (Sandbox Code Playgroud)
您可能想要学习一本好的C++书籍,因为您遇到的错误是该语言的基础.
类声明需要class
在类名之前有一个关键字:
class TPlayer
{
private:
// ...
Run Code Online (Sandbox Code Playgroud)
你需要这个,因为编译器需要知道你是在谈论a class
还是a struct
或a union
或者enum
等等.否则你最终会遇到很多错误.
您的成员函数也需要加上前缀TPlayer::
,就像您对构造函数和析构函数所做的那样.这些是必需的,以便编译器知道它们是TPlayer
类的一部分.
TPlayer::TPlayer()
{
}
TPlayer::~TPlayer()
{
}
void TPlayer::SetName(char *_name)
{
}
char TPlayer::GetName(void)
{
}
Run Code Online (Sandbox Code Playgroud)
有没有必要给delete
类成员,你没有分配自己.
~TPlayer::TPlayer()
{
// These are not needed. The variables name, hp, dmg, and wep
// were allocated when you created an instance of TPlayer. These
// will go away by themselves when the destructor is called.
//delete name;
//delete hp;
//delete dmg;
//delete wep;
// The exceptions to the above rule are things that are dynamically
// allocated. For example, you must delete[] everything that
// you new[]'ed, and you must fclose() all file handles you
// get from fopen(). If you consistently use the RAII idiom,
// then you won't have to worry about these "exceptions".
}
Run Code Online (Sandbox Code Playgroud)
实际上,现在很少需要delete
在应用程序代码中使用现代C++程序.在"资源获取就是初始化"(RAII)成语让你不必担心delete
-ing东西绝大多数的时间.标准库设施如std::vector
使用RAII习语来管理阵列存储器.
请注意,有关使用以下划线开头的标识符的规则.您可能需要了解它们.
为了通过示例学习,这里是一个有效的示例类:
foo.h中
#ifndef FOO_H
#define FOO_H
class Foo
{
public:
Foo();
~Foo();
void Set(int n);
int Get() const;
private:
int n;
};
#endif
Run Code Online (Sandbox Code Playgroud)
Foo.cpp中
#include "foo.h"
Foo::Foo() : n(0)
{
}
Foo::~Foo()
{
}
void Foo::Set(int n)
{
this->n = n;
}
int Foo::Get() const
{
return n;
}
Run Code Online (Sandbox Code Playgroud)