c++ - 从命名空间调用方法 - 声明标识符

标签 c++ variables namespaces

我之前问过关于这个程序的问题,但还是有疑问。我在 stdfax.h 文件中创建了一个命名空间,并试图从 main 调用函数。

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    deductions::getData(mStatus, nOfChildren, salary, contribPension);
    deductions::taxAmount(mStatus, nOfChildren, salary, contribPension);
}

命名空间如下所示:

#pragma once

#include <iostream>
#include <iomanip>

using namespace std;

namespace deductions
{
    const double marriedDeduction = 7000.00;
    const double singleDeduction = 4000.00;
    const double personalExemption = 1500.00;

    void getData(char& mStatus, int& nOfChildren, double& salary,
                 double& contribPension)   
    {cout << "\n\n  Enter marital status:  m or M (married), s or S (single):  ";
    cin  >> mStatus;

    if(mStatus == 'm' || mStatus == 'M') 
    {
        cout << "  Number of children:" << setfill(' ') << setw(40) << ' ';
        cin  >> nOfChildren;
    }    // end IF

    cout << setfill(' ');
    cout << "  Enter gross Salary:" << setw(40) << ' ';
    cin  >> salary;
    cout << "  Percentage of salary contributed to Pension (0 to 6):" << setw(6) << ' ';
    cin >> contribPension;
    cout << endl;}    // end getData( 

    double taxAmount(char mStatus, int nOfChildren, double salary,
             double contribPension)    
    {...}
};

错误是 main 中未声明的标识符。再一次,我试过摆弄它,但无法让它工作。对不起,如果这是一个愚蠢的错误;我正在使用其他人的代码,我一直在研究它,但它已经不再有意义了。

在 main 中声明变量会产生错误,即它们已在 stdfax 中定义。

最佳答案

您还没有声明作为参数传递的变量:

int main()
{
    deductions::getData(mStatus, nOfChildren, salary, contribPension);
    deductions::taxAmount(mStatus, nOfChildren, salary, contribPension);
}

您必须声明它们。例如:

int main()
{
    char mStatus;
    int nOfChildren;
    double salary, contribPension;
    deductions::getData(mStatus, nOfChildren, salary, contribPension);
    deductions::taxAmount(mStatus, nOfChildren, salary, contribPension);
}

关于c++ - 从命名空间调用方法 - 声明标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21581787/

相关文章:

c++ - 在 Qt Creator 的 CMake 项目中添加 CMake 项目作为子项目

c++ - Linux -> C++ 代码 : Object Serialization

c++ - 在 mixin 类型之间复制数据

c 链式困惑

c++ - 有没有办法将 if 语句应用于 c++ 中的所有变量

php - PSR-4 自动加载问题

c++ - "*&"在一起是什么意思?

python - 使用列表中的项目创建文件名 - for 循环

命名空间中的 C++ 前向声明和友元

c++ - 命名空间中函数特化的 undefined reference