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

T

ThaDoctor

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....
 
V

Victor Bazarov

ThaDoctor said:
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
 
A

Alan Woodland

ThaDoctor said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top