Function connection problem

Hi,

I have created a program which is made of 3 classes, put it in a level form of inheritance.

Everything worked flawless until I decided that it's better to simplify my main function by creating other functions that should be included there.

I have declared the functions, I've wrote them, and called them, but when I build the project it says that the instances etc aren't declared.

This is the code:
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <string>
using namespace std;
void instance(); void write(); void display(); void clear();

void instance()
{
	person p;
	student ps;
	graduator psg;
}

void display()
{
	p.display();
	ps.display();
	psg.display();
}

void write()
{
	p.write(000, "Robert", "Lucian", 16);
	ps.write(001, "Jessica", "Andreea", 6, 2009);
	psg.write(002, "Tata", "Mama", 11, 2008, "Comp lf", "Columbia", 2003);
}

void clear()
{
	p.clearP();
	ps.clearPS();
	psg.clearPSG();
}


class person
{
protected:
	int id, age;
	char first[16], last[16];
public:
	virtual void display()
	{
		cout << "ID: " << id << endl;
		cout << "First name: " << first << endl;
		cout << "Last name: " << last << endl;
		cout << "Age: " << age << endl;
	}
	virtual void write(int ID, char FIRST[], char LAST[], int AGE)
	{
		id = ID;
		age = AGE;
		strcpy_s(first, FIRST);
		strcpy_s(last, LAST);
	}
	void clearP()
	{
		id = age = 0;
		first[0] = last[0] = 0;
	}
};

class student : public person
{
protected:
	int graduation;
public:
	void display()
	{
		person::display();
		cout << "Graduation: " << graduation << endl;
	}
	void write(int ID, char FIRST[], char LAST[], int AGE, int GRADUATION)
	{
		person::write(ID, FIRST, LAST, AGE);
		graduation = GRADUATION;
	}
	void clearPS()
	{
		graduation = 0;
	}
};

class graduator : public student
{
protected:
	int undergrad;
	char undergradschl[64], major[64];
public:
	void write(int ID, char FIRST[], char LAST[], int AGE, int GRADUATION, char MAJOR[], char UNDEGRADSCHL[], int UNDERGRAD)
	{
		student::write(ID, FIRST, LAST, AGE, GRADUATION);
		strcpy_s(major, MAJOR);
		strcpy_s(undergradschl, UNDERGRADSCHL);
		undergrad = UNDERGRAD;
	}
	void display()
	{
		student::display();
		cout << "Major: " << major << endl;
		cout << "Undergrad school: " << undergradschl << endl;
		cout << "Undergrad graduation: " << undergrad << endl;
	}
	void clearPSG()
	{
		undergrad = 0;
		undergradschl[0] = major[0] = 0;
	}
};

int main()
{
	instance();
	write();
	display();
	clear();
	return 0;
}


What's the problem here?

Thank you,
RobertEagle
Last edited on
You need an instance of the object you want to call the function from:
1
2
MyClass test;
test.MyFunction();
I know what you are saying, but I wasn't referring at that.

If you look in the main function, you'll see that there are 4 functions. Those 4 functions include the instances, the writes the everything that connects the main function to the classes.

And here's the problem: it gives me the error that what is defined in those 4 functions (instance, display, etc) isn't declared.
Oh, I didn't see the first part where you declared the functions on their own. The instance of person in "instance()" is local to that function. As soon as the function returns person p will be destroyed. Display, Write and Clear have no idea the person object in Instance exists.
You can fix this multiple ways.
1.You can make the variables part of a class, since any member functions of a class can see all of that class's variables, but again, in this case you will need to define an instance of that class in main.

2.You can pass the person p as a variable to the other functions. (If you want to make actual changes to p, you'll either have to assign it to the return values of the functions [which will be of person type], or you will have to pass it as either a reference or a pointer [preferably reference if you are a beginner])

3.You can make person p global (Not recommended).

4.You can make it a static member of a class, and access it using the scope operator ('::') [I wouldn't recommend this either]

EDIT: I also missed this... Man, I'm off today. You need to define the three classes before your functions, otherwise the functions won't know they exist.
Last edited on
Man...can you show me a code by choosing for example the reference method?
I've done this hundreds of time, but when it come to instances I'm a little bit stuck.
1
2
3
4
5
6
7
8
9
10
11
12
//Declare person and such here

void display(person p)
{
p.display();
}

int main()
{
person p;
display(p);
}

In this example, you are NOT passing by reference. You declare a function which takes a person object, and calls the display function on it. The thing is that when person is "passed" to the function, you aren't actually giving function the same person that main has, but you are creating a new one with the exact same values. "display()" gets a COPY of main's person, which gets destroyed at the end of display.
So if you make changes to the person in display, it won't affect the one in main. This is where references come in handy. If you want to be able to actually change a value you pass to a function, you can pass it by reference:
 
void display(person &p) //Note the '&' 

This gives display a reference to the person object you pass to the function as the argument, so any changes made in the function will apply to the exact object you passed it.

Also, I find it hard to believe you've done this (Coding in C++?) hundreds of times and don't understand how to properly call functions. I don't mind helping you, but please don't lie to us. I apologize in advance if you meant something else by "done this hundreds of times".
Yes, I'm telling it again. I've done arguments for a lot of time(in c++ for goddamn). An I won't lie.

I had the same idea about arguments and references. And it didn't work. I googled before you gave me this reply and I was writing the same. Maybe I was absent-minded and I forgot sth to put.

Sincerely, I'm not a liar, and I don't like to lie! Anyway, I'll try again.

Thank you,
RobertEagle

EDIT: Yes, it does work.
Last edited on
Topic archived. No new replies allowed.