类标题,字符串未命名类型

时间:2015-09-12 01:43:39

标签: c++ class header

您好,我想完成我的作业。当我尝试分离一个类时,我有一个编译错误,然后再调用它。但整个测试功能正常。它在整个文本中都有类。基本上当我尝试将类与文本分开时,我有一条错误消息。

#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;
}`

但是当我把课程分开时,:

#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();

};

主档

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


int main()
{

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

    return 0;
}`

但是当我分开这个类时,我在codeblocks中遇到错误。请帮忙。

1 个答案:

答案 0 :(得分:1)

您忘了将using namespace std;放在Person.h中。

另外,你在Person.h上没有任何标题保护,这在一个简单的程序中不会引起问题,但是只要多个文件包含Person.h就会出现。