Thread: Compiler error C2370 and C2011

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    33

    Compiler error C2370 and C2011

    Working on a solution involving inheritance. The whole solution is pretty massive at this point so I'll just focus on the problem areas. I'm getting a lot of "redefinition" and "undefined class type" compiler errors, including C2370, 2011, 2504, and 2027, in Benefit.h, Employee.h (the constant members are a big occurance) I'm also getting 2027 and 2079 in EmployeeMain.cpp. with my Benefit and Employee object calls.

    Clearly I missed something in about how to code this correctly. Sadly the course textbook focuses on general OOP theory instead on the accompanying C++ syntax.

    Can anyone give me a hand with this?
    Thanks.

    Benefit.h

    Code:
    #include <iostream>
    #include <string>  
    #include <cstdlib>
    #include <iomanip>
    using namespace std;
    
    static const double Min_Life = 0;
    static const double Max_Life = 1000000;
    static const int Min_Vacation = 0;
    static const int Max_Vacation = 365;
    
    class Benefit
    {
    private:
        string HealthInsurance;
        double LifeInsurance;
        int Vacation;
        
    public:
        Benefit();
        Benefit(string health, double life, int vac);
        void displayBenefits();
        string getHealthInsurance();
        void setHealthInsurance(string health);
        double getLifeInsurance();
        void setLifeInsurance(double life);
        int getVacation();
        void setVacation(int vac);
    };
    Employee.h

    Code:
    #include "Benefit.h"
    
    const int Max_Depend = 10;
    const int Min_Depend = 0;
    const double Max_Salary = 250000;
    const double Min_Salary = 20000;
    const char Default_Gender = 'U';
    
    class Employee
    {
    protected:
        string FirstName, LastName;
        char Gender;
        int Dependent;
        double AnnualSalary;
        Benefit benefit;
    
    private:
        static int numEmployees;
        
    public:
        Employee();
        Employee(string fName, string lName, char gen, int depend, double salary, Benefit ben);
        double calculatePay();
        void displayEmployee();
        string getFirstName();
        void setFirstName(string fName);
        string getLastName();
        void setLastName(string lName);
        char getGender();
        void setGender(char gen);
        int getDependent();
        void setDependent(int depend);
        void setDependent(string depend);
        double getAnnualSalary();
        void setAnnualSalary(double salary);
        void setAnnualSalary(string salary);
        static int getnumEmployees();
        Benefit getBenefit();
        void setBenefit(Benefit ben);
    };
    EmployeeMain.cpp
    Code:
    #include "Hourly.h"
    #include "Salaried.h"
    
    void DisplayApplicationInformation();
    void DisplayDivider(string);
    string GetInput(string);
    void TerminateApplication();
    int Employee::numEmployees = 0;
    
    int main()
    {
        DisplayApplicationInformation();
        string input = "";
        
        DisplayDivider("      EMPLOYEE #1      ");
        cout << endl;
        
        Benefit benefit1("Atena", 250000, 45);
        Employee emp1("John","Jones",'M', 3, 100000, benefit1);
    
        emp1.Employee::displayEmployee();
    
        DisplayDivider("Number of Employees");
        cout << "Number of employees: " << Employee::getnumEmployees() << endl;
        cout << endl;
    
        DisplayDivider("      EMPLOYEE #2      ");
        cout << endl;
        
        Benefit benefit2("Blue Cross", 250000, 90);
        Salaried emp2("Mary", "Smith", 'F', 4, 50000, benefit2, 2);
        
        emp2.Salaried::displayEmployee();
    
        DisplayDivider("Number of Employees");
        cout << "Number of employees: " << Employee::getnumEmployees() << endl;
        cout << endl;
    
        DisplayDivider("      EMPLOYEE #3      ");
        cout << endl;
        
        Benefit benefit3("Cigna", 500000, 60);
        Hourly emp3("Tom", "Jones", 'M', 5, 25,    40, benefit3, "FT");
    
        emp3.Hourly::displayEmployee();
    
        DisplayDivider("Number of Employees");
        cout << "Number of employees: " << Employee::getnumEmployees() << endl;
        cout << endl;
    
        TerminateApplication();
        system("pause");
        return 0;
    }
    
    void DisplayApplicationInformation()
    {
        cout << endl;
        cout << "Welcome to the Employee Class Program" << endl;
        cout << "CIS247C, Week5 Lab" << endl;
        cout << "Developer: John Worley" << endl;
        cout << endl;
    }
    
    void DisplayDivider(string outputTitle)
    {
        cout << setw(12) << outputTitle << setw(12) << endl;
        cout << "-----------------------" << endl;
    }
    
    string GetInput(string input) //general input function; takes input as string
    {
        cout << "Enter " << input << ": ";   // no endl, in order to allow input on same line
        getline(cin, input);
        return input;
    }
    
    void TerminateApplication() // terminate program message
    {
        cout << "Thank you for using the Employee class program" << endl;
        cout << "Program terminating" << endl;
        cout << "Good-Bye" << endl;
        cout << endl;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > using namespace std;
    Don't put using in header files.

    If you get to the point of writing library code for other people, they might not want the whole of the std namespace along for the ride.
    Lookup "namespace pollution" for more info.

    > static const double Min_Life = 0;
    Remove the static keyword from all of these declarations.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Salem
    > static const double Min_Life = 0;
    Remove the static keyword from all of these declarations.
    Actually, the "redefinition" error lies with those declarations that do not have the static keyword. The problem is that these variable declarations define the variable, hence when the header is included in multiple source file, the same variable is defined multiple times, which is an error. With static, each source file has a different variable with the same name, hence there is no error, though it is potentially a waste of space.

    The straightforward solution is to remove the static keyword with the extern keyword (but keep the const), remove the initialisation, then define the variable in exactly one file (and initialise it there).

    However, these variables are actually constants associated with certain classes, so a better solution is to keep the static keyword and move them into the appropriate class definition. For integer static const member variables, the initialisation can actually be kept in the class definition itself, but for others, e.g., Max_Salary (of type double), the initialisation should be removed, and instead the class constant should be initialised in exactly one source file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Apart from the above, it is also a good idea to employ include guards in the header files.

    Definitions that appear in header files often result in either "redefinition" errors at compile time, or multiply defined symbols at link time.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    33
    Thanks for the input guys. I looked over what y'all suggested and have it compiled now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ error C2011
    By lionofgod in forum C++ Programming
    Replies: 7
    Last Post: 08-20-2011, 04:24 PM
  2. Replies: 7
    Last Post: 12-13-2010, 10:02 PM
  3. GCC compiler giving syntax error before 'double' error
    By dragonmint in forum Linux Programming
    Replies: 4
    Last Post: 06-02-2007, 05:38 PM
  4. Compiler error error C2065: '_beginthreadex; : undeclared identifier
    By Roaring_Tiger in forum Windows Programming
    Replies: 3
    Last Post: 04-29-2003, 01:54 AM
  5. fatal error C1001: INTERNAL COMPILER ERROR
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2002, 12:07 PM