【问题标题】:Unsure about structures that use pointers and inheritance [closed]不确定使用指针和继承的结构[关闭]
【发布时间】:2016-06-28 13:11:44
【问题描述】:

大家好,我对编码还是很陌生,所以请给我一些恩典,但我基本上有一个错误,这与我正在唱歌的结构有关,该结构包含我称为 Records 的对象。我不确定是否应该将此对象设为指针,因为当我执行程序编译时会出现运行时错误。我得到的错误是

对“Records::Records()”的未定义引用
1d 返回 1 个退出状态

当我在 Employee 结构中使用指向 Records 的指针时,我得到的运行时错误发生在第一次引用它时。

由于我对程序没有太多评论,这里是我的解释。它基本上是一个打卡/打卡程序。 Date 类是不言自明的。班次类实现了这一点,并在员工上班和下班时保持不变。 Records 类基本上保存了一周的班次对象工作,并将用于将此信息写入电子表格。

正如我所说,我对编码还很陌生,所以如果您对程序结构有任何提示,或者任何类似的提示,我很想听听。

ma​​in.cpp

*

#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <limits>
#include <fstream>
#include <ctime>
using namespace std;
#include "Date.h"
#include "Shift.h"
#include "Records.h"
bool AdminMenu(int id);
int EmployeeMenu(int id);
void printInfo(int id);
int findEmptySpot();
void addEmployee();
void printEmployeeList();
void generateEmployees();
void degenerateEmployees();
Date blankDate(1,1,1900,0,0);
struct employee{
    string name;
    bool adminStatus;
    int id;
    double wage;
    Records  r;
};
typedef struct employee Employee;
Employee data[100];
ifstream employeeList ("employeeList.txt");
int main()
{
    bool x = true;
    if (employeeList.is_open())
    {
        generateEmployees();
        employeeList.close();
    }
    else
        cout << "Unable to open file";
    Sleep(1000);
    while(x)
    {
        int input,i=0;
        cout<< "Enter your 4 digit id: ";
        cin>> input;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        std::system("cls");
        while(data[i].id!=input)
            i++;
            ;
        if(data[i].adminStatus ==true)
            x = AdminMenu(i);
        else
            EmployeeMenu(i);
    }
    employeeList.close();
    degenerateEmployees();
    return 0;
}
bool AdminMenu(int placeInArray)
{
    while(true)
    {
        time_t now = time(0); // get current time
        struct tm* tm = localtime(&now);
        Date blankDate(1,1,1900,0,0);
        Date currentDate(tm->tm_mon,tm->tm_mday,tm->tm_year,tm->tm_min,tm->tm_sec);
        std::system("cls");
        int input=0;
        cout<<"You are in the admin menu\n1. Print My info\n2. Add Employee\n3. Edit Employee\n4. Delete Employee\n5. Print Employee List\n6. Clock In\n7. Clock Out\n8. Close Program\n9. Finish"<<endl;
        cin>> input;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        if(input<1||input>9)
        {
            cout<< "Invalid input please try again"<<endl;
            Sleep(1000);
            std::system("cls");
        }
        else if(input==1)
        {
            std::system("cls");
            printInfo(placeInArray);
            getch();
            std::system("cls");
        }
        else if(input ==2)
        {
            addEmployee();
        }
        else if (input ==3)
        {
            std::system("cls");
            int placeToEdit, whatToEdit;
            cout << "Please look at the list of employees below and enter the number of the employee you would like to edit";
            printEmployeeList();
            cin>> placeToEdit;
            std::system("cls");
            cout<< "What do you want to change? \n1. Name\n2. ID\n3. Admin Status\n4. Clock In/Out";
            cin>> whatToEdit;
            if(whatToEdit==1)
            {
                std::system("cls");
                cout<<"Enter the employee's new name: ";
                cin >> data[placeToEdit].name;
                cout<< "\nEmployee's name adjusted successfully.";
                Sleep(1000);
            }
            else if(whatToEdit==2)
            {
                std::system("cls");
                cout<<"Enter the employee's new ID: ";
                cin >> data[placeToEdit].id;
                cout<< "\nEmployee's ID adjusted successfully.";
                Sleep(1000);
            }
            else if(whatToEdit == 3)
            {
                std::system("cls");
                cout<<"Enter the employee's new admin status: ";
                cin >> data[placeToEdit].adminStatus;
                cout<< "\nEmployee's Admin status adjusted successfully.";
                Sleep(1000);
            }
            else if(whatToEdit == 4)
            {
               /* std::system("cls");
                int status;
                cout<<"Would you like to clock them in or out?(0 or 1)";
                cin >> status;*/
            }
            else
            {
                std::system("cls");
                cout<< "Error: invalid input. You will be returned to the admin menu";
                Sleep(1000);
            }
        }
        else if(input ==4)
        {
            std::system("cls");
            int placeToEdit;
            cout << "Please look at the list of employees below and enter the number of the employee you would like to delete";
            printEmployeeList();
            cin>> placeToEdit;
            if( data[placeToEdit].id!= 0)
            {
                data[placeToEdit].name = '\0';
                data[placeToEdit].id = '\0';
                data[placeToEdit].adminStatus='\0';
                cout<< "\nEmployee successfully deleted.";
                Sleep(1000);
            }
            else
                cout<< "Error: invalid input. You will be returned to the admin menu";
        }
        else if(input ==5)
        {
            printEmployeeList();
            Sleep(1000);
        }
        else if (input == 6)
        {
            if(tm->tm_wday==1)
            {
                data[placeInArray].r.Monday.Shift::setBeginOFShift(blankDate);
            }
            else if(tm->tm_wday==2)
            {
                data[placeInArray].r.Tuesday.Shift::setBeginOFShift(blankDate);
            }
            else if(tm->tm_wday==3)
            {
                data[placeInArray].r.Wednesday.Shift::setBeginOFShift(blankDate);
            }
            else if(tm->tm_wday==4)
            {
                data[placeInArray].r.Thursday.Shift::setBeginOFShift(blankDate);
            }
            else if(tm->tm_wday==5)
            {
                data[placeInArray].r.Friday.Shift::setBeginOFShift(blankDate);
            }
            std::system("cls");
            cout<< "Successfully clocked in at";
            currentDate.Date::print();
        }
        else if (input == 7)
        {
            if(tm->tm_wday==1)
            {
                data[placeInArray].r.getMonday().setEndOFShift(currentDate);
            }
            else if(tm->tm_wday==2)
            {
                data[placeInArray].r.getTuesday().setEndOFShift(currentDate);
            }
            else if(tm->tm_wday==3)
            {
                data[placeInArray].r.getWednesday().setEndOFShift(currentDate);
            }
            else if(tm->tm_wday==4)
            {
                data[placeInArray].r.getThursday().setEndOFShift(currentDate);
            }
            else if(tm->tm_wday==5)
            {
                data[placeInArray].r.getFriday().setEndOFShift(currentDate);
            }
        }
        else if(input ==8)
        {
            std::system("cls");
            return false;
        }
        else if(input ==9)
        {
            std::system("cls");
            return true;
        }
    }
}
int EmployeeMenu(int placeInArray)
{
    int input;
    while(true)
    {
        time_t now = time(0); // get current time
        struct tm* tm = localtime(&now);
        Date blankDate(1,1,1900,0,0);
        Date currentDate(tm->tm_mon,tm->tm_mday,tm->tm_year,tm->tm_min,tm->tm_sec);
        cout<< "You are in the Employee Menu\n1. Print My info\n2. Clock in\n3. Clock out\n9. Finish";
        cin>> input;
        std::system("cls");
        if(input<1||input>9)
        {
            cout<< "Invalid input please try again"<<endl;
            Sleep(1000);
            std::system("cls");
        }
        else if(input==1)
            printInfo(placeInArray);
        else if (input == 2)
        {
            if(tm->tm_wday==1)
            {
                data[placeInArray].r.getMonday().setBeginOFShift(currentDate);
            }
            else if(tm->tm_wday==2)
            {
                data[placeInArray].r.getTuesday().setBeginOFShift(currentDate);
            }
            else if(tm->tm_wday==3)
            {
                data[placeInArray].r.getWednesday().setBeginOFShift(currentDate);
            }
            else if(tm->tm_wday==4)
            {
                data[placeInArray].r.getThursday().setBeginOFShift(currentDate);
            }
            else if(tm->tm_wday==5)
            {
                data[placeInArray].r.getFriday().setBeginOFShift(currentDate);
            }
        }
        else if (input == 3)
        {
            if(tm->tm_wday==1)
            {
                data[placeInArray].r.getMonday().setEndOFShift(currentDate);
            }
            else if(tm->tm_wday==2)
            {
                data[placeInArray].r.getTuesday().setEndOFShift(currentDate);
            }
            else if(tm->tm_wday==3)
            {
                data[placeInArray].r.getWednesday().setEndOFShift(currentDate);
            }
            else if(tm->tm_wday==4)
            {
                data[placeInArray].r.getThursday().setEndOFShift(currentDate);
            }
            else if(tm->tm_wday==5)
            {
                data[placeInArray].r.getFriday().setEndOFShift(currentDate);
            }
        }
        else if(input ==9)
        {
            std::system("cls");
            return 0;
        }
    }
}
void printInfo(int placeInArray)
{
    cout<<"Name: "<<data[placeInArray].name<<endl;
    cout<<"ID: "<<data[placeInArray].id<<endl;
    cout<<"Admin status: "<< data[placeInArray].adminStatus<<""<<endl;
}
int findEmptySpot()
{
     int i=0;
     while(data[i].id!=0)
            i++;
     return i;
}
void printEmployeeList()
{
    int x;
    for( x = 0; x<100; x++)
    {
        if( data[x].id!=0)
            cout<<"\n"<<x<<". "<<data[x].name<<endl;
    }
}
void addEmployee()
{
    int newPlace = findEmptySpot();
    std::system("cls");
    cout<<" Enter the employee's name: ";
    cin >> data[newPlace].name;
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout<<" Enter the employee's ID: ";
    cin >> data[newPlace].id;
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout<<" Enter the employee's admin status: ";
    cin >> data[newPlace].adminStatus;
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    std::system("cls");
    cout<< "Your new employee profile has been created!";
    printInfo(newPlace);
}
void generateEmployees()
{
        string n;
        bool a;
        int i, j =0;
        while (employeeList >>n>>i>>a )
        {
            data[j].name=n;
            data[j].adminStatus=a;
            data[j].id=i;
            j++;
        }
}
void degenerateEmployees()
{
    ofstream employeeList("employeeList.txt",ios::trunc);
    int x;
    for( x = 0; x<100; ++x)
    {
        if( data[x].id!=0)
            employeeList<< data[x].name<< " "<< data[x].id<< " "<< data[x].adminStatus<< "\n";``
    }
    employeeList<< "End 0 0";
    employeeList.close();
}

*

日期.cpp

#include <iostream>
#include "Date.h" // include Date class definition
using namespace std;

// constructor confirms proper value for month; calls
// utility function checkDay to confirm proper value for day
//-------------------------------------------------------------
Date::Date( int mn, int dy, int yr, int hr, int minu)
{
    month = mn;
    year = yr;
    day = dy ;
    hour = hr;
    minute=minu;
} // end Date constructor


Date::Date (const Date & dateToCopyFrom) // copy constructor
{
    month = dateToCopyFrom.month;
    day = dateToCopyFrom.day;
    year = dateToCopyFrom.year;
    hour = dateToCopyFrom.hour;
    minute = dateToCopyFrom.minute;
}

Date & Date::operator = (const Date & other) // assignmenrt operator
{
    this->month = other.month;
    this->day = other.day;
    this->year = other.year;
    this->hour = other.hour;
    this->minute= other.minute;
    return *this;
}

/* setDate date from the user */
void Date::setDate()
{
    bool flag = false;
    int mn,dy,yr,hr,minu;
    cin.clear();
    do
    {
        cout << "(month day year hour minute):\n";
        cin >> mn >> dy >> yr >> hr >> minu;
        year = yr;
        month = mn;
        day = dy;
        hour = hr;
        minute=minu;

        if(yr < 1900)
        {
            cout << "Cannot handle dates prior to 1900 AD" << endl;
            cout << "Try again!" << endl;
            flag = false;
        }
        else if(!isValidDate())
        {
            cout << "Invalid date" << endl;
            cout << "Try again!" << endl;
            flag = false;
        }
        else
            flag = true;
    }

    while (!flag);

}//end date constructor


// print Date object in form month/day/year
//-------------------------------------------------------------
void Date::print() const
{
    std::cout << hour<<":"<<minute<< "on"<< month << '/' << day << '/' << year;
} // end function print

// output Date object to show when its destructor is called
//-------------------------------------------------------------
Date::~Date()
{
} // end ~Date destructor

// utility function to confirm proper day value based on
// month and year; handles leap years, too
//-------------------------------------------------------------
int Date::checkDay( int testDay ) const
{
    static const int daysPerMonth[ monthsPerYear + 1 ] =
    { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    // determine whether testDay is valid for specified month
    if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
        return testDay;

    // February 29 check for leap year
    if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
                                          ( year % 4 == 0 && year % 100 != 0 ) ) )
        return testDay;

    std::cout << "Invalid day (" << testDay << ") set to 1.\n";

    return 1; // leave object in consistent state if bad value
} // end function checkDay


bool Date::operator>(const Date &dateOnRight) // greater than operator
{
    bool greater;

    int year = dateOnRight.getYear();
    if(dateOnRight.getYear() > year)
        greater = false;

    else if(dateOnRight.getYear() == year && dateOnRight.getMonth() > month)
        greater = false;

    else if(dateOnRight.getYear() == year && dateOnRight.getMonth() == month && dateOnRight.getDay() >= day)
        greater = false;

    else if(dateOnRight.getYear() == year && dateOnRight.getMonth() == month && dateOnRight.getDay() == day&& dateOnRight.getHour()>= hour)
        greater = false;

    else if(dateOnRight.getYear() == year && dateOnRight.getMonth() == month && dateOnRight.getDay() == day&& dateOnRight.getHour()== hour&& dateOnRight.getMinute()<= minute)
        greater = false;
    else
        greater = true;

    return greater;

}
// add one year to date
void Date::addYears(int nyears)
{
    year = year + nyears;
    //month = month;
    if (day == 29 && !isLeapYear(year))
    {
        day = 28;
    }//end if

}

/***********************************************/
bool Date::isLeapYear(int year)
{
    bool leapYear = false;
    if (year % 400 == 0)
    {
        leapYear = true;
    }
    else
    {
        if (year % 100 == 0)
        {
            leapYear = false;
        }
        else
        {
            if (year % 4 == 0)
                leapYear = true;
            else
                leapYear = false;
        }
    }
    // cout << "leapYear  " << leapYear << endl;
    return leapYear;

}

/************************************************/
bool Date::isValidDate()
{
    bool validDate;
    int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if(month < 1 || month > 12
            || day < 0 || !((isLeapYear(year) && month == 2 && day <= 29)
                            || (day <= monthDays[month - 1])))
        validDate = false;
    else
        validDate = true;
    // cout << "validDate  " << validDate << endl;
    return validDate;
}

日期.h

#ifndef DATE_H
#define DATE_H

class Date
{
public:
    static const int monthsPerYear = 12; // number of months in a year
    Date (int mn=1, int dy=1, int yr=1900, int hr=0, int minu =0); // constructors
    Date (const Date& dateToCopyFrom); // copy constructor
    Date & operator = (const Date & other); // assignmenrt operator
    void setDate ();
    int getYear() const {return(year);};
    int getMonth() const {return (month);};
    int getDay() const {return (day);};
    int getHour() const {return (hour);};
    int getMinute() const {return (minute);};
    void print() const; // print date in month/day/year format
    bool operator>(const Date &dateOnRight);
    void addYears(int nyears);

    ~Date(); // destructor provided to confirm destruction order
private:
    int month; // 1-12 (January-December)
    int day; // 1-31 based on month
    int year; // any year
    int hour;
    int minute;
    // utility function to check if day is proper for month and year
    int checkDay( int testDay ) const;
    bool isLeapYear(int year);
    bool isValidDate();
};


#endif

Shift.cpp

#ifndef DATE_H

#define DATE_H

class Date
{
public:
    static const int monthsPerYear = 12; // number of months in a year
    Date (int mn=1, int dy=1, int yr=1900, int hr=0, int minu =0); // constructors
    Date (const Date& dateToCopyFrom); // copy constructor
    Date & operator = (const Date & other); // assignmenrt operator
    void setDate ();
    int getYear() const {return(year);};
    int getMonth() const {return (month);};
    int getDay() const {return (day);};
    int getHour() const {return (hour);};
    int getMinute() const {return (minute);};
    void print() const; // print date in month/day/year format
    bool operator>(const Date &dateOnRight);
    void addYears(int nyears);

    ~Date(); // destructor provided to confirm destruction order
private:
    int month; // 1-12 (January-December)
    int day; // 1-31 based on month
    int year; // any year
    int hour;
    int minute;
    // utility function to check if day is proper for month and year
    int checkDay( int testDay ) const;
    bool isLeapYear(int year);
    bool isValidDate();
};


#endif

Shift.h

#include "Date.h"

#ifndef SHIFT_H
#define SHIFT_H

using namespace std;

class Shift: public Date
{
public:
    //Constructor
    Shift();//blankDate,blankDate,(9999),blankDate);
    Shift(Date in, Date out, int identity){beginOfShift=in; endOfShift=out; ident = identity;};

    void setBeginOFShift(Date x);
    void setEndOFShift(Date x);

    Date getBeginOfShift() const{return (beginOfShift);};
    Date getEndOfShift() const{return (endOfShift);};

    Date getLength();
    int getID() const{return (ident);};
    ~Shift();
private:
    Date beginOfShift;
    Date endOfShift;
    Date Length;
    int ident;

};

#endif // SHIFT_H

Records.cpp

#include<iostream>

using namespace std;

#include "Records.h"
#include "Shift.h"
extern Date blankDate;

void Records::setBeginningOfWeek(Date x)
{
    getBeginningOfWeek()=x;
}
void Records::setMonday(Shift x)
{
    getMonday()=x;
}
void Records::setTuesday(Shift x)
{
    getTuesday()=x;
}
void Records::setWednesday(Shift x)
{
    getWednesday()=x;
}
void Records::setThursday(Shift x)
{
    getThursday()=x;
}
void Records::setFriday(Shift x)
{
    getFriday()=x;
}

Records::~Records()
{

}

Records.h

#include "Shift.h"

#ifndef RECORDS_H
#define RECORDS_H

using namespace std;

class Records: public Shift
{
    public:

        Records(Date x){beginningOfWeek=x;};
        Records();
        void setBeginningOfWeek(Date x);
        void setMonday(Shift x);
        void setTuesday(Shift x);
        void setWednesday(Shift x);
        void setThursday(Shift x);
        void setFriday(Shift x);

        Date getBeginningOfWeek() const{return(beginningOfWeek);};
        Shift getMonday() const{return(Monday);};
        Shift getTuesday() const{return(Tuesday);};
        Shift getWednesday() const{return(Wednesday);};
        Shift getThursday() const{return(Thursday);};
        Shift getFriday() const{return(Friday);};
        ~Records();



        Date beginningOfWeek;
        Shift Monday;
        Shift Tuesday;
        Shift Wednesday;
        Shift Thursday;
        Shift Friday;
};

#endif // RECORDS_H

【问题讨论】:

  • 您的错误不是运行时错误,而是链接器错误。链接器找不到 Records 的构造函数,它可能在您的 .cpp 文件中丢失。
  • 如果不看自己做了什么,就很难猜出自己犯了什么错误。你能提供一个产生相同错误的最小代码示例吗?
  • 您似乎忽略了您的代码应该Minimal这一事实。如果您不学习如何正确提出问题,您可能不会对所获得的帮助感到满意。

标签: c++ inheritance data-structures


【解决方案1】:

好像您为 Records 声明了一个 构造函数,但没有定义它。

【讨论】:

  • 是的,我现在修好了。它已经被投票了很多,所以我现在不会得到答案:/
  • 支持和接受答案是 stackoverflow 表达感谢的方式。
猜你喜欢
  • 2021-08-11
  • 2015-03-26
  • 1970-01-01
  • 1970-01-01
  • 2019-08-31
  • 2013-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多