I'm trying to utilize header files and their source files but when I try to access the classes within them I come up with a bit of trouble, here's the code for my header file:
// person.h
namespace PersonFuncs
{
class Person;
void getValues ( Person& );
void setValues ( Person& );
}
And my headers source file:
// person.cpp
#include <iostream>
#include <string>
#include "person.h"
using namespace std;
namespace PersonFuncs
{
class Person
{
private:
string name; // Declaring string variable to hold person's name
int height; // Declaring integer variable to hold person's height
public:
string getName() const; // Reads from 'name' member variable
void setName ( string ); // Writes to the 'name' member variable
int getHeight() const; // Reads from the 'height' member variable
void setHeight ( int ); // Writes to the 'height' member variable
};
string Person::getName() const
{
return name;
}
void Person::setName ( string s )
{
if ( s.length() == 0 ) // If user does not input the name then assign with statement
name = "No name assigned";
else // Otherwise assign with user input
name = s;
}
int Person::getHeight() const
{
return height;
}
void Person::setHeight ( int h )
{
if ( h < 0 ) // If user does not input anything then assign with 0 (NULL)
height = 0;
else // Otherwise assign with user input
height = h;
}
void getValues ( Person& pers )
{
string str; // Declaring variable to hold person's name
int h; // Declaring variable to hold person's height
cout << "Enter person's name: ";
getline ( cin, str );
pers.setName ( str ); // Passing person's name to it's holding member
cout << "Enter height in inches: ";
cin >> h;
cin.ignore();
pers.setHeight ( h ); // Passing person's name to it's holding member
}
void setValues ( Person& pers )
{
cout << "The person's name is " << pers.getName() << endl;
cout << "The person's height is " << pers.getHeight() << endl;
}
}
Of which both compile with no errors at all! But with this bit of code below where as you can probably see I try to utilize the 'Person' class:
// Person_Database.cpp
#include <iostream>
#include "person.h"
using namespace std;
using namespace PersonFuncs
int main()
{
Person p1; // I get an error with this
setValues ( p1 );
cout << "Outputting user data\n";
cout << "====================\n";
getValues ( p1 );
return 0;
}
The compiler (which is MS Visual C++) error I get are:
'p1' uses undefined class
and
setValues cannot convert an int
getValues cannot convert an int
or something along those lines.
Anybody got any ideas in what I've done wrong? or is there a certain way to access the variables in a class?