0

Hi I'm trying to finish my homework. I have a compilation error when I try to separate a class, then call it later. But the whole test function works properly. It has the class within the whole text. Basically when i try to separate the class from the text, I have an error message.

#include <iostream>
#include<string>
using namespace std;

class Person
{
private:
 string alpha;
int beta;

public:
Person(string Name, int Age)
{
    alpha = Name;
    beta = Age;
}
string getName()
{
    return alpha;
}
int getAge()
{
    if (beta < 0)
    {   beta = 0;
        cout << "Error. A negative age cannot be entered. " << endl;
        }
    if (beta > 120)
    {
        cout << "Damn you're old. How many heart transplants have you had? You Vampire " << endl;
    }
    return beta;
}
void setName(string alpha)
{

}
void setAge(int beta);
void display();

};

int main()
{


Person Lu("Jess ", 22);
Person Rose("Gary ", 49);
cout << Lu.getAge() << "   " << Lu.getName() <<endl;
cout << Rose.getAge() << "   " << Rose.getName() << endl;
return 0;
}`

But when i separate the class,:

#include <iostream>
#include <string>

class Person
{
private:
   string alpha;
  int beta;

public:
    Person(string Name, int Age)
{
    alpha = Name;
    beta = Age;
}
string getName()
{
    return alpha;
}
int getAge()
{
    if (beta < 0)
    {   beta = 0;
        cout << "Error. A negative age cannot be entered. " << endl;
        }
    if (beta > 120)
    {
        cout << "Damn you're old. How many heart transplants have you had? You Vampire " << endl;
    }
    return beta;
}
void setName(string alpha)
{

}
void setAge(int beta);
void display();

};

Main file

#include <iostream>
#include "Person.h"
#include <string>
using namespace std;


int main()
{

Person Lu("Jess ", 22);
cout << Lu.getAge() << "   " << Lu.getName() <<endl;

    return 0;
}`

But when I separate the class i get an error in codeblocks. Please help.

Michael La
  • 51
  • 5

1 Answers1

1

You forgot to put using namespace std; in Person.h.

Also, you don't have any header guards on Person.h, which won't cause a problem in such a simple program, but will as soon as multiple files include Person.h.

Dominique McDonnell
  • 2,510
  • 16
  • 25
  • 1
    @MichaelLa I should mention that having `using namespace std;` in a header is very dangerous. See http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Dominique McDonnell Nov 25 '15 at 22:29