Trying to troubleshoot "out of scope" compiling error: CodeBlocks, Linux Fedora

Trying to troubleshoot "out of scope" compiling error: CodeBlocks, Linux Fedora

请帮助我了解此错误的来源 - 我无法判断它是 IDE 的代码还是设置。虽然 "out of scope" 问题很常见,但我搜索了答案,但没有找到任何帮助。

我正在 SoloLearn 上在线学习 C++ 课程。根据他们的建议,我已经在 Linux 中下载并设置了 Codeblocks,这样我就可以通过编写代码并在 IDE 中编译来学习课程,而不是简单地通过他们的浏览器在线 window.到目前为止一切都很好;0/

所以我正在学习 "composition" 的课程并且(再次遵循他们的建议)分解了他们的代码并创建了单独的文件来定义 constructors/classes。然而,尽管我相信我做的一切都是正确的,但我还是不断收到这个 "out of scope" 错误。

这是他们的代码,写在一页上。如果我将其作为单个文件复制并粘贴到 IDE 中,它会成功编译,并且我选择的终端 window 会弹出预期的输出:

#include <iostream>
using namespace std;

class Birthday {
public:
    Birthday(int m, int d, int y)
    : month(m), day(d), year(y)
    {  }
    void printDate()
    {
        cout<<month<<"/"<<day <<"/"<<year<<endl;
    }
private:
    int month;
    int day;
    int year;
};

class Person {
public:
    Person(string n, Birthday b)
    : name(n), bd(b)
    {  }
    void printInfo()
    {
        cout << name << endl;
        bd.printDate();
    }
private:
    string name;
    Birthday bd;
};

int main() {
Birthday bd(2, 21, 1985);
Person p("David", bd);
p.printInfo();
}

我现在正在做的是将第一个构造函数定义删除到单独的 .h.cpp 文件中,如下所示:

#ifndef BIRTHDAY_H
#define BIRTHDAY_H


class Birthday
{
public:
    Birthday(int  m, int d, int y);
    void printDate();

private:
    int month;
    int day;
    int year;
};

#endif // BIRTHDAY_H

#include "Birthday.h"
using namespace std;

Birthday::Birthday(int m, int d, int y)
: month(m), day(d), year(y)
{ }

void printDate()
    {
        cout<<month<<"/"<<day <<"/"<<year<<endl;
    }

无论我做什么,我都会清理、重新运行 项目文件。我已经删除并重新创建了它们。我已经重启了。但是每次我尝试构建时,我都会得到以下信息:

如果代码根本无法工作,我会更仔细地检查我的工具,但为什么将它复制到单独的文件中会给我这个错误?

您需要在 Birthday.cpp 或 Birthday.h 中包含 iostream。其次,printDate 的定义需要限定在 class 范围内:Birthday::printdate.