全局变量“ count”暧昧 [英] Global variable "count" ambiguous

查看:103
本文介绍了全局变量“ count”暧昧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <algorithm>
using namespace std;

int count = 0, cache[50];

int f(int n)
{  
    if(n == 2) count++;
    if(n == 0 || n==1) return n;
    else if (cache[n] !=- 1) return cache[n];
    else cache[n]= f(n-1) + f(n-2);
    return cache[n]; 
}

我在gcc 4.3.4中使用了此功能,并出现以下错误:

I used this function with gcc 4.3.4, and got the following error:

prog.cpp: In function ‘int f(int)’:
prog.cpp:38: error: reference to ‘count’ is ambiguous

在我的本地计算机(mingw32)上,我得到的错误是<一个href = http://picturepush.com/public/8610702 rel = noreferrer>这,尽管它不是 int'cache []'

On my local machine (mingw32), the error I got was this one, although it's not for int 'cache[]'.

任何原因?

推荐答案

问题是都是因为这里的第二行:

The problem is all because of the second line here:

#include <algorithm>
using namespace std;

使用命名空间std的行 来自< algorithm> 的名称,该名称还具有名为 count 的函数,并且在您的代码中,您声明了一个变量 count 。因此,模棱两可的错误。

The line using namespace std brings all the names from <algorithm> which also has a function called count, and in your code, you've declared a variable count. Hence the ambiguous error.

解决方案是从不使用命名空间std 编写。不好不好不好

The solution is to never write using namespace std. It is bad bad bad.

相反,请使用 std :: cout std :: cin std :: endl std :: count 等,在您的代码中。

Instead, use std::cout, std::cin, std::endl, std::count and so on, in your code.

这篇关于全局变量“ count”暧昧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆