Matthim
Matthim

Reputation: 1

How do i use overloading with friend function

I am new to coding with c++ and I am confused about how to fix the error message popping out with the code below. The error message reads:

/tmp/cc3Qc0nv.0: In function 'main':
main.cpp:(.text+0x73a): undefined reference to 'operator>>(std::istream&, Section_401k&)'
main.cpp:(.text+0x74d): undefined reference to 'operator>>(std::ostream&, Section_401k&)'
collect2: error: ld returned 1 exit status
/bin/bash: line 4: ./a.out: No such file or directory
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

class Section_401k {
    friend ostream& operator<<(ostream&, const Section_401k&);
    friend istream& operator>>(istream&, Section_401k&);
private:
    double annual_salary;
  double annual_salary_increase;
  double annual_rate_of_return;
  int current_age;
  int age_of_retirement;
  double current_balance;
  double contribution;
  double employer_match;
  double employer_max_contribution;
    double totalAccountValue;
public:
    Section_401k();
    ~Section_401k();
    void computeTotalAccountValue();
    void applyPercentage();
};

Section_401k :: Section_401k() {}
void Section_401k :: applyPercentage(){
    Section_401k anEmployee;
    cout << "Internal Revenue Code: Section 401(k)\n";
    cout << "Annual Salary ($): ";
    cin >> anEmployee.annual_salary;
    cout << "Annual Salary Increase (%): ";
    cin >> anEmployee.annual_salary_increase;
    anEmployee.annual_salary_increase /= 100.0;
    cout << "Annual Rate of Return (%): ";
    cin >> anEmployee.annual_rate_of_return;
    anEmployee.annual_rate_of_return /= 100.0;
    cout << "Current Age: ";
    cin >> anEmployee.current_age;
    cout << "Age of Retirement: ";
    cin >> anEmployee.age_of_retirement;
    cout << "Current 401k Balance ($): ";
    cin >> anEmployee.current_balance;
    cout << "Contribution to 401k (%): ";
    cin >> anEmployee.contribution;
    anEmployee.contribution /= 100.0;
    cout << "Employer Match (%): ";
    cin >> anEmployee.employer_match;
    anEmployee.employer_match /= 100.0;
    cout << "Employer Max Contribution (%): ";
    cin >> anEmployee.employer_max_contribution;
    anEmployee.employer_max_contribution /= 100.0;
}

Section_401k :: ~Section_401k(){}
void Section_401k:: computeTotalAccountValue() {
    Section_401k anEmployee;
    double rate = anEmployee.annual_rate_of_return;
    double CB = anEmployee.current_balance;
    int n = anEmployee.age_of_retirement - anEmployee.current_age;
    double YC = (anEmployee.annual_salary * anEmployee.contribution) / 12.0;
    double EC = YC * anEmployee.employer_match;
    anEmployee.totalAccountValue = (CB * pow((1 + (rate / 12.0)), 12 * n)) +
        ((YC + EC) * (pow((1 + (rate / 12.0)), 12 * n) - 1)) / (rate / 12.0) *
        (1 + (rate / 12.0));
    cout << "\n";
    cout << "Your 401(k) Summary\n";
    cout << "Annual Salary ($): " << anEmployee.annual_salary << "\n";
    cout << "Annual Salary Increase (%): " << anEmployee.annual_salary_increase << "\n";
    cout << "Annual Rate of Return (%): " << anEmployee.annual_rate_of_return << "\n";
    cout << "Current Age: " << anEmployee.current_age << "\n";
    cout << "Age of Retirement: " << anEmployee.age_of_retirement << "\n";
    cout << "Current 401k Balance ($): " << anEmployee.current_balance << "\n";
    cout << "Contribution to 401k (%): " << anEmployee.contribution << "\n";
    cout << "Employer Match (%): " << anEmployee.employer_match << "\n";
    cout << "Employer Max Contribution (%): " << anEmployee.employer_max_contribution << "\n";
    cout << "Total Account Value at Age " << anEmployee.age_of_retirement << ": $" << anEmployee.totalAccountValue << "\n";
}

int main() {
Section_401k anEmployee;
cout << setprecision(2) << fixed;
cin >> anEmployee;
cout << anEmployee;
return 0;
}

Upvotes: 0

Views: 20

Answers (0)

Related Questions