0

I'm new to C++ and learning about Inheritance and Polymorphism. We require to write an employee project that have 4 types of employee (BasePlusCommission, CommisisonEmployee, Salaried and TipWorker). My project has one main() class that I used switch method for each type of employee. I got stuck on TipWorker where we have to do Polymorphism. Here what I got so far.

int main()
{
void virtualViaPointer(const Employee * const);
    {
                cout << "Enter First Name: " << endl;
                cin >> firstName;
                cout << "Enter Last Name: " << endl;
                cin >> lastName;
                cout << "Enter SSN: " << endl;
                cin >> SSN;
                if (SSN.length() == 9)
                {
                    SSN = true;
                }
                else
                {
                    cout << "Please enter SSN again with 9 digits only:" << endl;
                    cin >> SSN;
                }
                cout << "Enter wages: " << endl;
                cin >> wage;
                cout << "Enter hours: " << endl;
                cin >> hours;
                cout << "Enter tips: " << endl;
                cin >> tips;

                TipWorker employee4(firstName, lastName, SSN, wage, hours, tips);
                employee4.print();
                cout << fixed << setprecision(2);

                vector < Employee * > employees(1);
                employees[0] = &employee4;

                cout << "Employee processed polymorphically via dynamic binding: \n\n";
                cout << "Virtual function calls made off base-class pointers:\n\n";
                for (const Employee *employeePtr : employees)
                    virtualViaPointer(employeePtr);
                void virtualViaPointer(const Employee * const baseClassPtr)
                {
                    baseClassPtr->print();
                    cout << "\nEarned $" << baseClassPtr->earnings() << "\n\n";
                }
                    break;
      }
}

When I run the project, I came up with this error:

error C2601: "virtualViaPointer": local function definitions are illegal

void virtualViaPointer(const Employee * const baseClassPtr)
                    {
                        baseClassPtr->print();
                        cout << "\nEarned $" << baseClassPtr->earnings() << "\n\n";
                    }

Can anyone please help me? Thank you so much!

Phu Nguyen
  • 35
  • 1
  • 7
  • 1
    it is quite self explanatory. You defined virtualViaPointer inside function main, and in C++ you can not have local functions(excluding lambdas, but they have different syntax) – Creris Apr 23 '15 at 18:46
  • possible duplicate of [C++ can we have functions inside functions?](http://stackoverflow.com/questions/4324763/c-can-we-have-functions-inside-functions) – Cory Kramer Apr 23 '15 at 18:48
  • so how can I fix this error? Because I'm not really quite understand what you mean – Phu Nguyen Apr 23 '15 at 18:49
  • Nevermind, I think I got it now. I fixed. But thank you for your ideas – Phu Nguyen Apr 23 '15 at 18:51

2 Answers2

3

You may not define one function inside an other function. Any function definition shall be outside any other function definition.

Place the definition of virtualViaPointer outside the body of main.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I got that part now. It worked when I placed it outside my main(). But how can I increase the income 20%. Can you please help me on that? – Phu Nguyen Apr 23 '15 at 18:58
  • @Phu Nguyen I do not know how you calculate the income but is not it enough to multiply it by 1.2? – Vlad from Moscow Apr 23 '15 at 19:01
  • LoL, I asked so many dumb questions. I actually solved it. I multiplied by 1.2 but used it in a wrong place. Thanks for your response too :) – Phu Nguyen Apr 23 '15 at 19:04
1

You can have a declaration of a function inside a function, but no definition(!) of that function. However, you can have a local struct/class or lambda in the function:

#include <iostream>
void f()
{
  void print(); // mostly useless - maybe a most vexing parse error
  struct Print {
    static void apply(const char* s) { std::cout << s << '\n'; }
  }
  Print::apply("Hello");
  auto lambda = [] (const char* s) { std::cout << s << '\n'; };
  lambda("World");
}

Note: A local struct does not require C++11 (Also, it may look nicer while debugging)