0

I have 2 classes in the same file employee and employeeException. If i define a constructor for employee , i will be unable to define a constructor for employeeException , vice versa . Trying to define constructors for both classess would cause the following compilation error :

no matching function call to employee

#include <iostream>
#include <string>

using namespace std;

class employee
{
    public:

            double operator + (employee);
            bool operator == (employee);

            employee(int);
            double getSalary();

    private:

           double salary;

};

class employeeException
{
    public:

    employeeException(string);

    void printmessage();

    private:

    employee e;
    string message;



};



int main()
{  
  employee A(400);
  employee B(400);

  employee C = A+B;

  if ( A == B)
  {
    cout<<"Yes";
  }
  else
  {
    cout<<"No";
  }

  cout<<C.getSalary();
}

employee::employee(int salary)
{
    this->salary = salary;
}


double employee::operator + (employee e)
{
    double total;

    total = e.salary + this->salary;

    return total;    
}


double employee::getSalary()
{
    return this->salary;
}

bool employee::operator == (employee e)
{
    if ( e.salary == this->salary)
    {
        return true;
    }
    else
    {
        return false;
    }
}   

employeeException::employeeException(string message)
{

    this->message = message;
}

void employeeException::printmessage()
{
    cout<<endl
        <<this->message
        <<endl;
}

Questions

1) From the above , it seems that we cannot define constructors of 2 different class in the same file , are there any way to overcome this

2) Can someone give me a explaination why we cant define constructors of 2 different class in the same file

Additional info

I am using Quincy 2005 to compile the code

You can use this online compiler: http://www.compileonline.com/compile_cpp0x_online.php

Conclusion It seems i have to add the default constructor employee() for it to work , thanks to everyone for helping me out one way or another

7
  • 3
    There is no such restriction. What is the error you are receiving? One problem I see is that employeeException contains an employee and employee has no default constructor. Therefore, constructing an employeeException would generate an error... but that has nothing to do with the file that the constructor is implemented in. Commented Nov 28, 2013 at 3:24
  • 1
    Should work. What is the actual error you get? Commented Nov 28, 2013 at 3:24
  • 1
    which line do you get the error? Commented Nov 28, 2013 at 3:26
  • at the line employeeException::employeeException(string message) { this->message = message; } Commented Nov 28, 2013 at 3:27
  • You can copy paste the code and uncomment out the lines to see the error Commented Nov 28, 2013 at 3:28

2 Answers 2

3

You need to call the constructor of employee in the constructor of employeeException.

employeeException::employeeException(string message)
  : employee(42)
{
     ...
}

Because the default employee constructor is private as soon as you declare another one.

Sign up to request clarification or add additional context in comments.

3 Comments

employeeException is not a subclass of employee.
@John3136: employeeException has an employee member, and employee does not have a default constructor, so that member can only be constructed by using the initialization list.
Yep - you got me. Too much time away from C++
0

I suspect (but haven't confirmed) that your error comes from the third line in your main function:

employee C = A + B;

The expression on the right invokes the operator+(employee) method, which returns a double. The expression on the left will attempt to construct a new employee object. However, you haven't defined an employee constructor that takes a double (or a default constructor and assignment operator that takes a double). There is only one that takes an integer.

The easiest solution is probably to change your constructor to take a double parameter instead of an int, especially because it is stored as a double inside the class anyway.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.