参考' '很暧昧

时间:2015-04-01 09:48:41

标签: c++

我很抱歉,但我不知道为什么这个算法不起作用。 编译时的错误是:"参考'功能'暧昧"并在 y = function()行,我在调用函数

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define PI 3.141
float function(int g, int m, int s, float z)
{
    using namespace std;
    z = (g + m/60.0 + s/3600.0)*PI/180.0;
    return z;
}
int main()
{
     using namespace std;

    float y;
    int g,m,s;

    cout << "g = ";
    cin >> g;
    cout <<"m = ";
    cin >> m;
    cout<<"s= ";
    cin >>s;

    y = function();
    cout << "y= " << y << endl;
    //cout<< (g + m/60.0 + s/3600.0)*PI/180.0 << endl;
    return 0;
}

Vers2 - 更新:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define PI 3.141
float function(int g, int m, int s)
{
    //using namespace std;
    float z = (g + m/60.0 + s/3600.0)*PI/180.0;
    //std::cout << z <<std::endl;
    return z;
}
int main()
{
    // using namespace std;

    float y;
    int g,m,s;

    std::cout << "g = ";
    std::cin >> g;
    std::cout <<"m = ";
    std::cin >> m;
    std::cout<<"s= ";
    std::cin >>s;

    function();
  //  std::cout << "y= " << y << std::endl;
    //cout<< (g + m/60.0 + s/3600.0)*PI/180.0 << endl;
    return 0;
}

3 个答案:

答案 0 :(得分:10)

std中有一个成员function,您将其插入到命名空间中。避免使用using namespace std;;你可以用这种方式导入你需要的东西:

using std::cout;
using std::cin;

答案 1 :(得分:1)

当我使用“ prev”作为Node *类型的全局变量时,我遇到了类似的错误。在我的案例中,只需使用“ prevv”对它进行重新命名即可。

这主要是由于您使用的某些库中存在“变量或函数”的名称。

答案 2 :(得分:0)

我无法重现您的错误消息(对于您的3个不同编译器的任何版本),但您的代码的基本问题是您显然假设主要功能中的g,m,s - 变量是当你打电话给function()时,它会自动用作参数,因为它们碰巧有相同的名字。

事实并非如此!

main中的变量和function()的参数列表中的变量是完全独立的实体。调用函数并传递正确值的正确方法是:

y=function(g,m,s);

这基本上将存储在主g,m,s变量中的值复制到g,m,s参数中,这些参数在函数内部访问,在函数完成后,它会复制存储在变量中的值&#34;返回&#34;从函数(此处z)到变量y

无论您是否使用using namespace std;,这都应该有效,因为您的功能具有完全不同的签名,但我仍然强烈建议为您的功能选择其他名称。

我希望这听起来不像是一种侮辱,但我强烈建议您阅读一本关于c ++编程的介绍性书籍,因为您似乎错过了该语言的基本概念。