1

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?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • That's not how C++ works. I would strongly recommend a Good Book, as a complete answer to your question would amount to a basic C++ tutorial. – Kerrek SB Dec 24 '11 at 15:51
  • 1
    You have to declare the *entire* class in the header in order to use it from other source files (like `Person_Database.cpp`). Those Good Books that Kerrek mentioned are listed [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Cody Gray - on strike Dec 24 '11 at 15:53
  • It's probably also a good idea to explicitly provide a public default constructor for the class, so you won't get any surprises from the one provided by the compiler. – David R Tribble Dec 24 '11 at 18:06

1 Answers1

1

A complete declaration of the class Person must be available to the compiler when it compiles your main.

You should put the class definition in your header file (and include it in your main file). You can leave the implementation of the member functions in a separate .cpp file though.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • How do I do that? I am quite new to C++ –  Dec 24 '11 at 15:59
  • You cut all the code `class Person { ... };` from your `person.cpp` file and paste it to `person.h` over the `class Person;` line. – Mat Dec 24 '11 at 16:03
  • This next question is slightly related to the question above, how would I be able to add things like D.O.B, or education etc. to the class? Would I have to make another class marked 'Date' and then reference to it in 'Person'? or is there another way that must be done? –  Dec 24 '11 at 17:12
  • A custom class for dates is one possibility. A library that provides pre-built data types for that is another (boost probably has something). You really should follow Kerrek and Cody Gray's advice: pick up a good C++ book. There are (nearly) always several ways to do things in C++ - which one is best depends on the situation. If you want to make good decisions, you need to understand the language. – Mat Dec 24 '11 at 17:15
  • Of course it won't have an answer to your specific question. It will teach you all the tools you have at your disposal. With that knowledge, you need to find the concept that works best for your situation. You'll need to think about that yourself, and try things out. You'll learn more by experimenting than by getting spoon-fed answers here (or elsewhere). – Mat Dec 24 '11 at 17:23