Global variable "count" ambiguous
Asked Answered
R

5

30
#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?

Reinaldoreinaldos answered 30/6, 2012 at 6:6 Comment(18)
@DavidSchwartz this is C++ code , but same problem has happened in C also.Reinaldoreinaldos
@DavidSchwartz : Doesn't matter.Surf
@Mat [this] (ideone.com/Ic6KD) is the link at ideone . It's giving same error.Reinaldoreinaldos
@Reinaldoreinaldos : 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.Surf
@Mat : That edit is pretty drastic, especially if the OP is convinced they have C code.Surf
@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.)Contributory
this is error message i am getting: "52 C:\Users\rachit\Desktop\testprograms\codes\DPTopDown\tets.cpp `count' undeclared (first use this function) "Reinaldoreinaldos
@Contributory : 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.)Surf
@user801154: that's not the error that shows in your ideone link.Contributory
@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.Contributory
@ildjarn: Your ESP failed you this time. Turned out it was a C++ specific bug involving a collision with std::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++.)Planking
@MAT it is same code producing the error i mentioned on my local machine and it's giving different error on ideone.Reinaldoreinaldos
@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.)Planking
@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.Surf
@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 #includes 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.Planking
let me clear the things here : this is code => 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)=> 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.Reinaldoreinaldos
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.Reinaldoreinaldos
possible duplicate of Using std NamespacePiquant
M
91

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.

Metamathematics answered 30/6, 2012 at 6:20 Comment(9)
Even if I removed the '#include<algorithm>' line from my code, error persists .Reinaldoreinaldos
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.Somnambulate
@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.Metamathematics
@user801154: You are wrong. Here's your original code. Here is your code with the suggested changes. The latter compiles and runs just fine.Scrubland
picturepush.com/public/8610599 this is link of image producing same error.Reinaldoreinaldos
@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 .Reinaldoreinaldos
@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...Contributory
@Mat: Very observant. I wonder if that was an honest mistake, or if the OP was trying to pull the wool over our eyes.Corydon
@Contributory picturepush.com/public/8610702 Saved Code . And yeah that was an honest mistakeReinaldoreinaldos
Q
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.

Quixote answered 29/10, 2020 at 5:56 Comment(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...Cabob
B
0

enter image description here

A picture is worth a thousand words

Bolter answered 27/7 at 5:11 Comment(1)
It's conflict with std::countBolter
M
-1

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

Marlonmarlow answered 12/11, 2020 at 18:6 Comment(1)
This answer does not answer the question.Bamboozle
R
-1

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

Radiothorium answered 6/11, 2021 at 9:2 Comment(1)
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.Colton

© 2022 - 2024 — McMap. All rights reserved.