An Issue With Using Classes in my Program

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.

Error Report:


1>------ Build started: Project: Survival, Configuration: Debug Win32 ------
1>  Survival.cpp
1>Survival.obj : error LNK2001: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall person::findname(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?findname@person@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V23@@Z)
1>Survival.obj : error LNK2001: unresolved external symbol "public: int __thiscall person::findattack(int)" (?findattack@person@@QAEHH@Z)
1>Survival.obj : error LNK2001: unresolved external symbol "public: int __thiscall person::findhealth(int)" (?findhealth@person@@QAEHH@Z)
1>c:\users\william\documents\visual studio 2010\Projects\Survival\Debug\Survival.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Code:

(Things that were highlighted red by my compiler for my code will now be underlined in the code below)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "stdafx.h"
#include <Windows.h>
#include <time.h>
#include <string>
#include <iostream>
using namespace 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";}else if(c==2){n="Bob";}else if(c==3){n="Jonny";}else if(c==4){n="Billy";}else if(c==5){n="Robert";}
	return n;
}

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)
int findattack(a)

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/


Last edited on
This isn't an error, but you really should've made your #define into a stand-alone function.

Also, Moschops and Peter 87 are right about the problems with the program at http://www.cplusplus.com/forum/beginner/59887/#msg323432 , Moschops, you wise guy. I'd listen to them.

-Albatross
Last edited on
Also, Moschops and Peter 87 are right. I'd listen to them.


I just don't hear this sort of thing enough. I'm going to quote this as a background watermark on my CV :)
Thanks guys! I fixed it and now I can continue on programming. :)
Topic archived. No new replies allowed.