我如何使用友元函数重载

问题描述

我是使用 C++ 编码的新手,我对如何修复使用以下代码弹出的错误消息感到困惑。 错误信息如下:

/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;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...