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

时间:2014-02-05 15:46:05

标签: 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中定义。

2 个答案:

答案 0 :(得分:3)

您尚未声明作为参数传递的变量:

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

答案 1 :(得分:2)

请声明您的方法中使用的标识符......

deductions::getData(mStatus, nOfChildren, salary, contribPension);