我已经设法通过将#include for part_time.h移到#include列表的顶部来解决这个问题。为什么这会产生影响,我不知道。
我遇到一个手动对象构造问题,在将我的程序拆分为makefile后调用 。每个.cpp都有一个包含它的.h,而main.cpp也包含每个.h。
相关代码如下。错误是:
main.cpp:在函数'void add_part_time()'中: main.cpp:166:2:错误:在此范围内未声明'part_time' part_time part_time1(name,forklift,annual_leave,sick_leave); ^
main.cpp:166:12:错误:在'part_time1'之前预期';' part_time part_time1(name,forklift,annual_leave,sick_leave);
我注意到了一些奇怪的事情。如果我删除;在sick_leave定义行之后,我得到的唯一错误是同一个询问丢失的错误;而另一个错误消失了。
#include <string>
using namespace std;
class employee
{
public:
static int count;
employee (std::string name);
string name;
~employee();
};
employee::employee (string set_name)
{
name = set_name;
}
employee::~employee()
{
}
class dockhand: public employee
{
public:
dockhand (string set_name, bool set_forklift);
float start_shift;
bool forklift;
float payrate;
~dockhand();
};
dockhand::dockhand (string set_name, bool set_forklift) : employee (set_name)
{
forklift = set_forklift;
start_shift = 4.00;
}
dockhand::~dockhand()
{
}
class part_time: public dockhand
{
public:
part_time (string set_name, bool set_forklift, int annual_leave, int sick_leave);
float end_shift;
int annual_leave;
int sick_leave;
~part_time();
};
part_time::part_time (string set_name, bool set_forklift, int annual_leave, int sick_leave) : dockhand (set_name, set_forklift)
{
end_shift = 8.00;
payrate = 22.00;
}
part_time::~part_time()
{
}
void add_part_time()
{
string name;
bool forklift;
int annual_leave;
int sick_leave;
name = "bob";
forklift = true;
annual_leave = 2;
sick_leave = 3;
part_time part_time1(name, forklift, annual_leave, sick_leave);
}
然而,完全相同的格式在下面用另一个类编译得很好。
void add_casual()
{
string name;
bool forklift;
name = "bob";
forklift = true;
casual casual1(name, forklift);
}
我对问题是什么感到困惑。注释掉构造线会使它编译,因此它确实存在。
编辑更改part_time part_time1(name,forklift,annual_leave,sick_leave);对于临时构造函数调用的行使得它可以使用makefile进行编译。因此,即使包含正确,该特定行仍然存在问题。
答案 0 :(得分:0)
我已经设法通过将#include for part_time.h移到#include列表的顶部来解决这个问题。绝对没有其他改变。为什么这会产生影响,我不知道。