Problems with my first CPP program - header and source

Im spending my saturday night not dressed up for halloween but rather sitting trying to learn CPP :D

anyways could someone please help me, Below I have included my source code, basically when I try compiling this form the terminal I'm getting a lot of errors, basically stating that the variables "name, ho, etc" are not declared, yet I have included my header file, so could someone pleas shave a look at this and maybe tell me what is missing? Thank you so much in advance guys!

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

and here is my source file:

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

And so forth, yet its telling me that for example, name etc has not been as shown below:

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

Upvotes: 1

Views: 156

Answers (2)

In silico
In silico

Reputation: 52187

You may want to pick up a good C++ book to learn from, as the things you're getting wrong are fundamental to the language.


Class declarations need a class keyword preceeding the class name:

class TPlayer 
{ 
private:
    // ...

You need this because the compiler needs to know if you're talking about a class or a struct or a union or an enum, etc. Otherwise you end up with lots of errors like you got.


Your member functions also need to be prefixed with TPlayer::, like how you did with the constructors and destructor. These are needed so that the compiler knows that they are part of the TPlayer class.

TPlayer::TPlayer()
{
}

TPlayer::~TPlayer()   
{ 
}

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

char TPlayer::GetName(void) 
{ 
} 

There's no need to delete class members that you didn't allocate yourself.

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

In fact, modern C++ programs nowadays rarely need to use delete in application code. The "Resource Acquisition Is Initialization" (RAII) idiom allows you to not have to worry about delete-ing things a vast majority of the time. Standard library facilities like std::vector use the RAII idiom to manage the array memory.


Note that there are rules regarding the use of identifiers beginning with underscores. You may need to be aware of them.


For the sake of learning by example, here's an valid sample class:

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

Upvotes: 7

Ed Swangren
Ed Swangren

Reputation: 124732

it should be class TPlayer, not TPlayer. That will confuse the compiler and there's no telling what errors you will get after it is encountered.

Also, your member function definitions need to be prefixed by TPlayer::, i.e.,

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

Upvotes: 1

Related Questions