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

时间:2011-10-29 22:36:18

标签: c++ oop

我星期六晚上没有为万圣节打扮,而是试图学习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 */

这是我的源文件:

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

等等,但它告诉我,例如,姓名等没有如下所示:

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

2 个答案:

答案 0 :(得分:7)

您可能希望选择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) 
{ 
} 

您不需要自己分配的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".
}

事实上,现在的现代C ++程序很少需要在应用程序代码中使用delete"Resource Acquisition Is Initialization" (RAII)成语允许您在绝大多数情况下不必担心delete。像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;
}

答案 1 :(得分:1)

应该是class TPlayer,而不是TPlayer。这会使编译器感到困惑,并且无法确定遇到它后会得到什么错误。

此外,您的成员函数定义需要以TPlayer::为前缀,即

TPlayer::SetName( const char* name ) {
    // ...
}