having a little problem with some code for a little game I am creating.

Discussion in 'C++' started by ThaDoctor, Sep 28, 2007.

  1. ThaDoctor

    ThaDoctor Guest

    Hi.

    I am quite new to C++ so I think I would ask here what I am doing
    wrong with this code.
    I am writing a little game in a text console, but here is something
    that is in no way related to the game but when I am compiling this it
    yields an error....
    Why is that

    #include<iostream>
    using namespace std;
    class Test
    {

    private:
    int life;

    public:
    void player_test_life()
    {
    if(life <= 0)
    {
    cout << "Your dead" << endl;
    }
    else
    {
    }
    }
    void player_minus_life()
    {
    life=100;
    while(life != 0)
    {
    --life;
    Test::player_test_life();

    }
    }
    };

    int main()
    {
    Test::player_minus_life();
    return 0;
    }


    Greetings Tobias, I am a new member of this group....
    ThaDoctor, Sep 28, 2007
    #1
    1. Advertising

  2. ThaDoctor wrote:
    > I am quite new to C++ so I think I would ask here what I am doing
    > wrong with this code.
    > I am writing a little game in a text console, but here is something
    > that is in no way related to the game but when I am compiling this it
    > yields an error....


    Do we have to guess what error it yields? Is that the game?

    > Why is that


    Because there must be some error in the code...

    >
    > #include<iostream>
    > using namespace std;
    > class Test
    > {
    >
    > private:
    > int life;
    >
    > public:
    > void player_test_life()
    > {
    > if(life <= 0)
    > {
    > cout << "Your dead" << endl;


    "You're dead"

    > }
    > else
    > {
    > }
    > }
    > void player_minus_life()
    > {
    > life=100;
    > while(life != 0)
    > {
    > --life;
    > Test::player_test_life();
    >
    > }
    > }
    > };
    >
    > int main()
    > {
    > Test::player_minus_life();


    You cannot call a non-static member function without an instance of
    the class. Create an instance and use the '.' operator to call your
    member function

    > return 0;
    > }
    >
    >
    > Greetings Tobias, I am a new member of this group....


    Read the FAQ before posting your next post, please.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 28, 2007
    #2
    1. Advertising

  3. ThaDoctor

    Giff Guest

    Re: having a little problem with some code for a little game I amcreating.

    ThaDoctor wrote:

    >
    > int main()
    > {
    > Test::player_minus_life();
    > return 0;
    > }


    It seems like you forgot to instantiate an object of the class Test.
    Giff, Sep 28, 2007
    #3
  4. Re: having a little problem with some code for a little game I amcreating.

    ThaDoctor wrote:
    > Hi.
    >
    > I am quite new to C++ so I think I would ask here what I am doing
    > wrong with this code.
    > I am writing a little game in a text console, but here is something
    > that is in no way related to the game but when I am compiling this it
    > yields an error....
    > Why is that
    >
    > #include<iostream>
    > using namespace std;
    > class Test
    > {
    >
    > private:
    > int life;
    >
    > public:
    > void player_test_life()
    > {
    > if(life <= 0)
    > {
    > cout << "Your dead" << endl;
    > }
    > else
    > {
    > }
    > }
    > void player_minus_life()
    > {
    > life=100;
    > while(life != 0)
    > {
    > --life;
    > Test::player_test_life();
    >
    > }
    > }
    > };
    >
    > int main()
    > {
    > Test::player_minus_life();

    Try:
    Test player;
    player.player_minus_life();

    instead - you're trying to call a non-static method without an instance
    to call it on.

    Alan
    Alan Woodland, Sep 28, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BlackHawke
    Replies:
    12
    Views:
    1,564
    Andrew Thompson
    Jan 26, 2004
  2. judith
    Replies:
    0
    Views:
    1,644
    judith
    Nov 1, 2006
  3. Max Kubierschky
    Replies:
    10
    Views:
    1,839
    pabloreda
    Mar 31, 2007
  4. Advertiser for `2D Games Development Central`

    {Game Development} 2D Game Development Central

    Advertiser for `2D Games Development Central`, May 7, 2008, in forum: Java
    Replies:
    2
    Views:
    410
    RedGrittyBrick
    May 8, 2008
  5. Advertiser for `2D Games Development Central`

    {Game Development} 2D Game Development Central

    Advertiser for `2D Games Development Central`, May 7, 2008, in forum: C++
    Replies:
    0
    Views:
    389
    Advertiser for `2D Games Development Central`
    May 7, 2008
Loading...

Share This Page