Can't get simple "write" function to work

Hello all, first post here having a bit of trouble figuring out what i am doing wrong.

I'll start by saying what i have...

I am creating an address book, so far i have only been able to implement the accepting of age and name values...

1
2
3
4
5
6
7
Person::Person()  //Default Constructor
{
	string _name = "";
	int _age = 0;
	int age = 0;
	string name = "";
}


That is located in my Person.cpp file

my header file contains;

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
class Person
{
public:
	Person(void);
	~Person(void);

	string name;
	int age;
	

	

	string getName()
	{
		return _name;
	}

	int getAge()
	{
		if(_age > 0)
		{
		return _age;
		}
	}

	string setName()
	{
		return name;
	}
	
	int setAge()
	{
		return age;
	}
	
	Person::Person(int age, string name)
	{
		_age = age;
		_name = name;
								
	}

	
	 void write()
	{
		cout << "This is the age:  " << _age << endl;
		cout << "This is the name:  " << _name << endl;
	}
	


private:
	string _name;
	int _age;
	

	

};


and my main contains;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main(int argc, char **argv) {

		
	Person(5,"Max");
	
	void write();
	
	

	


	int keypress; cin >> keypress;

	

	
}


Sorry for long post first of all, but from here you can see i have a write function in my person header file, and i attempt to call it in main. However my program does not do anything...I have been able to test that the instance i placed into main work (Person(5,"Max");) as i put the cout data into my construct and it printed out correctly.

Can anyone see anything blatently wrong i have done as to why i cannot call my write function?

Thanks
Bump...everywhere i look online says this should be working. No clue why it won't call the function and print out.
You are not actually initializing the data members in the default constructor. You are just declaring some local variables that you give values but never use.

This code should not compile because on line line 36 you have Person:: which should not be there.

in main, line 4 creates a temporary Person that is never used. What you want is probably to create a named object.
Person p(5,"Max");
Line 6 is a function declaration. To call the write() function on the Person object:
p.write();
Last edited on
Topic archived. No new replies allowed.