Object Constructor Call Not Declared In This Scope

2019-07-18 15:35发布

问题:

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.

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: 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);

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

I'm stuck as to what the problem is. Commenting out the construct line makes it compile so it's definitely something there.

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.

回答1:

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.