I'm having a difficult time setting up classes in a text-based game I planned to make for myself. The point of this game is to build up with a few individuals and try to survive in the wild, while this all takes place at the dawn of Man. Though I've only just started, I reached a problem. I wanted it all to be organized with classes and such, but I can't get the created class variables to work in the class's functions. I also should mention that I'm using Windows 7 64 bit with Microsoft Visual C++ 2010 Express as my compiler and computer.
I don't really know what else to explain aside from the error report and showing off the code.
#include "stdafx.h"
#include <Windows.h>
#include <time.h>
#include <string>
#include <iostream>
usingnamespace std;
#define random int c = rand()%max+min
class person
{
//private:
public:
int h; //Health
int a; //Attack
bool g; //Gender
string n; //Name
int findhealth(int h);
int findattack(int a);
string findname(string n);
};
class building
{
private:
int h; //Health
string b; //Name
};
int findhealth(h)
{
int max = 10, min = 5;
srand((NULL));
random;
h = c;
return h;
}
int findattack(a)
{
int max = 5, min = 1;
srand((NULL));
random;
a = c;
return a;
}
string findname(n)
{
int max = 5, min = 1;
srand((NULL));
random;
if(c==1){n="Joe";}elseif(c==2){n="Bob";}elseif(c==3){n="Jonny";}elseif(c==4){n="Billy";}elseif(c==5){n="Robert";}
returnn;
}
int main()
{
cout << "Welcome to my survival game!\n";
cout << "Vertical axis: y0, y1, y2, y3...\n";
cout << "Horizontal axis: x0, x1, x2, x3...\n";
person p1;
p1.findhealth(p1.h);
p1.findattack(p1.a);
p1.findname(p1.n);
cout << "Greetings! My name is " << p1.n << "!\n";
cout << "My attack is " << p1.a << "!\n";
cout << "My health is " << p1.h << "!\n";
char ff;
cin >> ff;
return 0;
}
I hope you can figure it out for me. Thanks for your time!
One problem is that when you define a member function outside the class body you need to write ClassName:: before the function name and also specify the type of the parameters. int person::findhealth(int h)
This is not how to define a function. Did you mean something like
int findattack(int a) ?
Likewise all the others.
Furthermore, your functions have nothing at all to do with your classes. They're all independent functions. Your class functions have to be clearly marked as belonging to that class. Read up on how to make classes: http://www.cplusplus.com/doc/tutorial/classes/