c++ - 类头,字符串不命名类型

标签 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;
}`

但是当我分开类(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();

};

主文件

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


int main()
{

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

    return 0;
}`

但是当我分离类时,我在代码块中遇到错误。请帮忙。

最佳答案

你忘了把 using namespace std; 放在 Person.h 中。

此外,您在 Person.h 上没有任何 header 保护,这不会在如此简单的程序中引起问题,但一旦多个文件包含 Person.h 就会出现问题。

关于c++ - 类头,字符串不命名类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32534323/

相关文章:

html - 在 li of 2 classes (HTML, CSS) 中选择一个类

c++ - 设置具有多个成员变量的类的私有(private)变量的方法

css - 标题 + 视频 slider 比屏幕大

C++ Visual Studio Unicode 混淆

c++ - 为什么我需要在 qt 中进行前向类声明?

c++ - 避免使用默认参数警告 'Unreferenced Formal Parameter'

node.js - Express + Request 中途更改 header

ruby - 如何在我的类上动态设置 HTTParty 配置参数?

c++ - 3次握手丢包

c++ - 如何从文件读取数据到 vector ?