29
#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]; 
}

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

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

Any reason why?

Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • @DavidSchwartz this is C++ code , but same problem has happened in C also. –  Jun 30 '12 at 06:15
  • @DavidSchwartz : [Doesn't](http://ideone.com/qNKQI) [matter](http://ideone.com/6cEXW). – ildjarn Jun 30 '12 at 06:15
  • @Mat [this] (http://ideone.com/Ic6KD) is the link at ideone . It's giving same error. –  Jun 30 '12 at 06:15
  • 3
    @user801154 : How is 'variable `count` is undeclared' the same as 'reference to `count` is ambiguous'? Post your real error message, not some poor from-memory approximation of it. – ildjarn Jun 30 '12 at 06:16
  • @Mat : That edit is pretty drastic, especially if the OP is convinced they have C code. – ildjarn Jun 30 '12 at 06:19
  • @ildjarn: check out the OP's link. It's C++. I just added the minimum of context to actually trigger the bug. (Error message pasted from the ideone link.) – Mat Jun 30 '12 at 06:20
  • this is error message i am getting: "52 C:\Users\rachit\Desktop\testprograms\codes\DPTopDown\tets.cpp `count' undeclared (first use this function) " –  Jun 30 '12 at 06:20
  • @Mat : Except that link doesn't contain the error the OP is actually getting... (That is not the OP's code, he just thinks the errors look the same apparently.) – ildjarn Jun 30 '12 at 06:21
  • @user801154: that's not the error that shows in your ideone link. – Mat Jun 30 '12 at 06:21
  • @ildjarn: hum... so we had the original question that didn't contain the error the OP mentionned, and an (unrelated?) piece of C++ code that does contain an error, but not the error the OP is asking about... voting to close. – Mat Jun 30 '12 at 06:25
  • 1
    @ildjarn: Your ESP failed you this time. Turned out it was a C++ specific bug involving a collision with [std::count](http://www.cplusplus.com/reference/algorithm/count/), so it did matter. You may wish to rethink how you determined that it didn't matter. (And this is why you should never use both the C and C++ tags unless you are specifically comparing C and C++.) – David Schwartz Jun 30 '12 at 06:36
  • @MAT it is same code producing the error i mentioned on my local machine and it's giving different error on ideone. –  Jun 30 '12 at 06:42
  • @ildjarn: The issue is the same with either the original code or the later code. There was no way to tell from the original question whether the issue was C-specific, C++-specific, or applicable to both, that's an important distinction and one of the first steps to figuring out what's going on, and having both tags actively interfered with figuring that out. (As did you by stating that it didn't matter.) – David Schwartz Jun 30 '12 at 07:01
  • @David : But it _didn't_ matter with the code the _OP_ actually posted. I've seen no reason so far to think that Mat's edit is valid, and the links in my first comment empirically back up my case. – ildjarn Jun 30 '12 at 07:10
  • @ildjarn: You couldn't tell if it mattered with the code the OP actually posted. It wasn't a complete program. You couldn't tell what `#include`s or `using` declarations might have come before it. There was no way to know what other factors were relevant, which was why I was asking. – David Schwartz Jun 30 '12 at 07:38
  • let me clear the things here : this is code => http://picturepush.com/public/8610732 Error message I was getting on local machine(mingw32 g++) : 'count' variable undeclared Error i was getting on ideone (g++ 4.3.4)=> http://ideone.com/EXv7j My mistakes:1. I thought both the errors were same. 2. I tagged the C/C++ on it and thought that error can come in both languages, without checking in 'C' , but error doesn't come in C. –  Jun 30 '12 at 07:47
  • What I conclude : It may be possible that count varible is also declared any of header file(stl_algo.h) I am using in C++ code, which explains the error on ideone. And dev-C++ may have messed with the actual error code by compiler and show self created error message, and that's why showing 'count' varible undeclared. –  Jun 30 '12 at 07:48
  • possible duplicate of [Using std Namespace](http://stackoverflow.com/questions/1265039/using-std-namespace) – CB Bailey Jun 30 '12 at 08:50

4 Answers4

88

The problem is all because of the second line here:

#include <algorithm>
using namespace std;

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.

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

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

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Even if I removed the '#include' line from my code, error persists . –  Jun 30 '12 at 06:23
  • Even if you have using namespace std you can explicitly say which count you mean by writing std::count for the function or ::count for the variable. – jcoder Jun 30 '12 at 06:24
  • 4
    @user801154: Sorry. I don't believe you. If there is still error, that means, there is more problem (and most likely the error in this case is something else). Why dont you do what I said? Remove `using namespace std` line, and use `std::cout` and `std::cin` etc. – Nawaz Jun 30 '12 at 06:27
  • 7
    @user801154: You are wrong. Here's [your original code.](http://ideone.com/awkOZ) Here is [your code with the suggested changes.](http://ideone.com/p3hDf) The latter compiles and runs just fine. – Nicol Bolas Jun 30 '12 at 06:30
  • http://picturepush.com/public/8610599 this is link of image producing same error. –  Jun 30 '12 at 06:40
  • 2
    @Nawaz , yeah i got your point that 'count' may be declared in header file i have included and if I change the name of variable to 'countOne', it's working fine . –  Jun 30 '12 at 06:45
  • 2
    @user801154: in that screenshot, your `tets.cpp` file is updated but not saved, and the compiler complains about `count` undeclared on line 14 when there is no `count` on that line, and a second error on line 28 when your code is shorter than 28 lines... – Mat Jun 30 '12 at 06:45
  • @Mat: Very observant. I wonder if that was an honest mistake, or if the OP was trying to pull the wool over our eyes. – Benjamin Lindley Jun 30 '12 at 06:54
  • @Mat http://picturepush.com/public/8610702 Saved Code . And yeah that was an honest mistake –  Jun 30 '12 at 07:15
1

I think I may have figured this out. I have found that removing the using namespace std doesn't help, but when I change the name of the variable to something which is less common, like count can be changed to cnt or some personal versions like knt or isCycle. I don't exactly know what is the reason behind this.

itskarad
  • 105
  • 1
  • 9
  • 1
    This hints, that in one of the headers being included someone does some macro definition with the name `count`. Even namespaces do not protect you from evil macros... – BitTickler Nov 12 '20 at 18:17
0

yeah idk but changing the name to less common variable name works fine in case of mine

JOGI
  • 11
  • 1
0

simply change the variable name as it 'count' matches the internal keyword while we declare using namespace std.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 06 '21 at 09:15