[英]Trying to troubleshoot “out of scope” compiling error: CodeBlocks, Linux Fedora
Please help me understand the source of this error - I'm unable to tell if it's the code or the settings for the IDE. 请帮助我理解此错误的根源-我无法确定这是IDE的代码还是设置。 Although "out of scope" questions are very common I have searched the answers but nothing found that helps.
尽管“超出范围”的问题很常见,但我搜索了答案,但没有发现有什么帮助。
I'm following a C++ course online at SoloLearn. 我正在SoloLearn在线上学习C ++课程。 Further to their suggestion I've downloaded and set up Codeblocks in Linux so that I can follow the course by writing the code and compiling it inside the IDE rather than simply online via their browser window.
根据他们的建议,我已经在Linux中下载并设置了Codeblock,这样我就可以通过编写代码并在IDE中进行编译来遵循该课程,而不仅仅是通过他们的浏览器窗口在线进行。 Everything's been fine until now ;0/
到目前为止一切都很好; 0 /
So I'm following a lesson on "composition" and (again following their suggestions) have broken down their code and created separate files to define constructors/classes. 因此,我正在上一堂关于“组成”的课程,并且(再次遵循他们的建议)分解了他们的代码,并创建了单独的文件来定义构造函数/类。 However, despite believing I'm doing everything correctly, I keep getting this one "out of scope" error.
但是,尽管相信我做的一切正确,但是我仍然收到此“超出范围”错误。
Here is their code, written on a single page. 这是他们的代码,写在一页上。 If I copy and paste into the IDE as a single file it compiles successfully, and my chosen terminal window pops up with the expected output:
如果将我作为单个文件复制并粘贴到IDE中,则编译成功,并且弹出的所选终端窗口将显示预期的输出:
#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();
}
What I'm now doing is removing the first constructor definition into separate .h
and .cpp
files, like so: 我现在正在做的是将第一个构造函数定义删除到单独的
.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
and 和
#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;
}
Whatever I do, I've cleaned, re-run the project files. 无论我做什么,我都清理了,然后重新运行项目文件。 I've deleted and recreated them.
我已经删除并重新创建了它们。 I've rebooted.
我已经重启了。 But every time I try building, I get the following:
但是,每次尝试构建时,都会得到以下信息:
If the code had never worked at all I'd be looking harder at my tools, but why is it that copying it into separate files gives me this error? 如果代码根本无法正常工作,我会更加努力地查看我的工具,但是为什么将其复制到单独的文件中却会出现此错误呢?
You need to include iostream
in either Birthday.cpp or Birthday.h. 您需要在Birthday.cpp或Birthday.h中包括
iostream
。 Second, the definition of printDate
needs to be scoped to the class: Birthday::printdate
. 其次,
printDate
的定义必须printDate
以下类: Birthday::printdate
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.