gpt4 book ai didi

c++ - 我的第一个 CPP 程序的问题 - 标题和源代码

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:05 25 4
gpt4 key购买 nike

我周六晚上没有为万圣节打扮,而是坐着学习 CPP :D

无论如何有人可以帮助我,下面我已经包含了我的源代码,基本上当我尝试编译这个形式的终端时我遇到了很多错误,基本上说明变量“name,ho等”不是已声明,但我已经包含了我的头文件,所以有人可以看看这个并告诉我缺少什么吗?非常感谢你们!

#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 */

这是我的源文件:

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

等等,但它告诉我,例如,name etc 没有如下所示:

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()’:

最佳答案

您可能想拿起a good C++ book从中学习,因为你犯错的地方是语言的基础。


类声明需要一个 class 关键字在类名之前:

class TPlayer 
{
private:
// ...

你需要这个是因为编译器需要知道你是在谈论一个class还是一个struct或者一个union或者一个enum 等。否则你最终会遇到很多错误。


您的成员函数也需要以 TPlayer:: 为前缀,就像您对构造函数和析构函数所做的那样。这些是必需的,以便编译器知道它们是 TPlayer 类的一部分。

TPlayer::TPlayer()
{
}

TPlayer::~TPlayer()
{
}

void TPlayer::SetName(char *_name)
{
}

char TPlayer::GetName(void)
{
}

没有必要删除不是您自己分配的类成员。

~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".
}

事实上,现在的现代C++程序很少需要在应用程序代码中使用delete"Resource Acquisition Is Initialization" (RAII) idiom 让你在绝大多数时候不必担心 delete -ing 事情。 std::vector 等标准库工具使用 RAII 习惯用法来管理数组内存。


请注意 there are rules regarding the use of identifiers beginning with underscores .您可能需要了解它们。


为了通过示例学习,这里有一个有效的示例类:

foo.h

#ifndef FOO_H
#define FOO_H

class Foo
{
public:
Foo();
~Foo();

void Set(int n);
int Get() const;

private:
int n;
};

#endif

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

关于c++ - 我的第一个 CPP 程序的问题 - 标题和源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7942129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com