在此范围中未声明对象构造函数调用 [英] Object Constructor Call Not Declared In This Scope

查看:160
本文介绍了在此范围中未声明对象构造函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经设法通过将#include for part_time.h移动到#include列表的顶部来自己解决这个问题。为什么这会产生影响,我不知道。

I have managed to solve this myself by moving the #include for part_time.h to the top of the #include list. Why this made a difference, I have no idea.

我遇到一个问题,将我的程序分成makefile 。每个.cpp都有一个包含它的.h,main.cpp也包括每个.h。

I'm having an issue with one manual object construct call after splitting my program into a makefile. Each .cpp has an include for it's .h, and main.cpp includes every .h as well.

相关代码如下。错误是:

The relevant code is below. The errors are:


main.cpp:在函数'void add_part_time()':
main.cpp:166:2 :error:'part_time'未在此范围内声明
part_time part_time1(name,forklift,annual_leave,sick_leave);
^

main.cpp: In function ‘void add_part_time()’: main.cpp:166:2: error: ‘part_time’ was not declared in this scope part_time part_time1(name, forklift, annual_leave, sick_leave); ^

main.cpp:166:12:error:expected';'before'part_time1'
part_time part_time1(name,forklift,annual_leave ,sick_leave);

main.cpp:166:12: error: expected ‘;’ before ‘part_time1’ part_time part_time1(name, forklift, annual_leave, sick_leave);

我注意到的奇怪的事情。如果我删除了;后的sick_leave定义行,那么唯一的错误我得到是同一个问关于丢失;而另一个错误消失。

Something weird I've noticed. If I delete the ; after the sick_leave definition line, then the only error I get is the same one asking about the missing ; and the other error disappears.

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

然而,完全相同的格式编译正好在下面与另一个类。

Yet the exact same format compiles just fine below with another class.

void add_casual()
{
    string name;
    bool forklift;
    name = "bob";
    forklift = true;
    casual casual1(name, forklift);
}

我遇到了什么问题。

EDIT 更改part_time part_time1(name,forklift,annual_leave,sick_leave) ;线到休闲构造函数调用使它与makefile编译良好。因此,即使包含正确的,这个特定的行仍然有问题。

EDIT Changing the part_time part_time1(name, forklift, annual_leave, sick_leave); line to the casual constructor call makes it compile fine with a makefile. So even with the includes being correct there is still something wrong with that particular line.

推荐答案

将#include for part_time.h移动到#include列表的顶部。绝对没有别的改变。为什么这会产生影响,我不知道。

I have managed to solve this myself by moving the #include for part_time.h to the top of the #include list. Absolutely nothing else was changed. Why this made a difference, I have no idea.

这篇关于在此范围中未声明对象构造函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆