Implementing all member functions of a class

Was wondering if someone would be able to assist me with this c++ homework assignment I am having an issue with? Thanks in advance to anyone who is able to assist me with this. I keep getting the same errors after changing the program code.

[/code]
//The following program will, with user input display their name and age
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>

class Person
{
public:
/**
Constructs a person with a name and age.
*/
Person();

/**
Reads the persons name and age.
*/
void read();
Person(string pname, int page);
string get_name() const;
int get_age() const;
private:
string name;
int age; // 0 if unknown
};


using namespace std;

Person::Person()
{
name;
age;
}

#endif


int main()
{
Person pers;
pers.get_name;
pers.get_age;
cout <<"Name: " << pers.get_name();
cout <<"Age: " << pers.get_age();

system("PAUSE");

return 0;
}

[/code]


1> Person.cpp
1>c:\users\olson\documents\visual studio 2012\projects\age\olson, k lab10\person.cpp(24): error C2061: syntax error : identifier 'string'
1>c:\users\olson\documents\visual studio 2012\projects\age\olson, k lab10\person.cpp(24): error C2535: 'Person::Person(void)' : member function already defined or declared
1> c:\users\olson\documents\visual studio 2012\projects\age\olson, k lab10\person.cpp(13) : see declaration of 'Person::Person'
1>c:\users\olson\documents\visual studio 2012\projects\age\olson, k lab10\person.cpp(25): error C2146: syntax error : missing ';' before identifier 'get_name'
1>c:\users\olson\documents\visual studio 2012\projects\age\olson, k lab10\person.cpp(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\olson\documents\visual studio 2012\projects\age\olson, k lab10\person.cpp(25): warning C4183: 'get_name': missing return type; assumed to be a member function returning 'int'
1>c:\users\olson\documents\visual studio 2012\projects\age\olson, k lab10\person.cpp(28): error C2146: syntax error : missing ';' before identifier 'name'
1>c:\users\olson\documents\visual studio 2012\projects\age\olson, k lab10\person.cpp(28): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\olson\documents\visual studio 2012\projects\age\olson, k lab10\person.cpp(37): error C2065: 'name' : undeclared identifier
1>c:\users\olson\documents\visual studio 2012\projects\age\olson, k lab10\person.cpp(47): error C3867: 'Person::get_name': function call missing argument list; use '&Person::get_name' to create a pointer to member
1>c:\users\olson\documents\visual studio 2012\projects\age\olson, k lab10\person.cpp(48): error C3867: 'Person::get_age': function call missing argument list; use '&Person::get_age' to create a pointer to member
1>
closed account (zb0S216C)
First, you're using "string" before introducing it. You need "using namespace std" before using "string":

1
2
3
4
5
using namespace std; // Try to avoid this.
class SomeClass
{
  string Name_; // Use "std::string" instead.
};

I don't know what you're trying to do in this code:

1
2
3
4
5
Person::Person()
{
name;
age;
}

It's neither initialisation nor a declaration, but a statement which will only be omitted from the final program. I'm going to safely assume that you meant to initialise both "name" and "age". If this is the case then you need to use the initialiser-list, which looks like this:

1
2
3
4
Person::Person( )
  : name( ), age( 0 )
{
}

When calling a function, make sure you have a set of parentheses after the function's symbol, like so:

 
SomeFunction( ); // Call 

If you don't have those parentheses, the compiler will return the address of the function instead.

Wazzak
Last edited on
This program is for a homework assignment. I am a newbie so to speak to programming. I am trying to find a tutor so I am able to get help with this hands on instead of online. Any help is good though and I appreciate it
I think what you meant to say was "Thanks, Framework". :-S
Thanks a bunch Framework.

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
//The following program will, with user input display their name and age 

#include<iostream>
#include<string>

using namespace std;

class Person {
public:
Person();
Person(string pname, int page);
string get_name() const;
int get_age() const;
private:
string name;
int age; // 0 if unknown
};

Person::Person(){
age=0;
}

Person::Person(string pname, int page){
name=pname;
age=page;
}

string Person::get_name() const{return name;}
int Person::get_age() const{return age;}

int main(){
Person A("Joseph",32);
Person B;
cout<<"Person A created with 'Joseph',32 and Person B created with default constructor.\n";
cout<<"A has a name of "<<A.get_name()<<" and age of "<<A.get_age()<<".\n";
cout<<"B has a name of "<<B.get_name()<<" and age of "<<B.get_age()<<".\n";

system("PAUSE");
return 0;
}
Last edited on
Topic archived. No new replies allowed.