1

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()’:
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • You should tell us what command you're using to compile – Dan Oct 29 '11 at 22:38
  • You don't want to delete variables that are not allocated. What lines are the errors on? You also declare name as char then treat it as char*. Maybe also you need "class" in front of TPlayer in the header file. – Jim Rhodes Oct 29 '11 at 22:40

2 Answers2

7

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;
}
Community
  • 1
  • 1
In silico
  • 51,091
  • 10
  • 150
  • 143
  • Really? I'm very sorry ill try this now and post back asap – Casper TheFriendlyHost Oct 29 '11 at 22:40
  • Thank you so much btw I really appreciate the help form you Wizards, but do they have to be Prefixed with TPlayer:: even though I included the "#include "TPlayer.h" header file? – Casper TheFriendlyHost Oct 29 '11 at 22:54
  • @CasperTheFriendlyHost: Yes. It's to tell the compiler that those functions belong to `TPlayer`. For example, you may have another class called `TEnemy` with a member function that's also called `SetName()`. Without `TPlayer::`, how can the compiler know if you're talking about `TPlayer::SetName()` or `TEnemy::SetName()`? – In silico Oct 29 '11 at 22:57
  • 1
    @CasperTheFriendlyHost: Again, I recommend that you pick up [a good introductory C++ book](http://stackoverflow.com/questions/388242), read through it and do the exercises, etc. They will cover these kings of things and much more. – In silico Oct 29 '11 at 22:59
  • I get you :) thank you btw, but like I said, I'm still getting a lot of errors so could someone please paste up a working solution> if possible? – Casper TheFriendlyHost Oct 29 '11 at 23:00
  • @Casper TheFriendlyHost: Nobody here on Stack Overflow can read your mind. What errors are you getting? – In silico Oct 29 '11 at 23:01
  • Guys, may I say, please I'm so sorry as I know you wizards have much more technically madness to solve but thank you so much for taking the time to help me, and the above post helps me paint a much cleaner picture of it all so thank you v much :) – Casper TheFriendlyHost Oct 29 '11 at 23:28
1

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 ) {
    // ...
} 
Ed S.
  • 122,712
  • 22
  • 185
  • 265