Global variable "count" ambiguous

51,385

Solution 1

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.

Solution 2

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.

Share:
51,385

Related videos on Youtube

Eitan T
Author by

Eitan T

I have extensive background in communications and DSP. I had been working a great deal in MATLAB and C++ before I got immersed in developing software in C for embedded systems.

Updated on November 06, 2021

Comments

  • Eitan T
    Eitan T over 2 years
    #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?

    • Admin
      Admin almost 12 years
      @DavidSchwartz this is C++ code , but same problem has happened in C also.
    • ildjarn
      ildjarn almost 12 years
      @DavidSchwartz : Doesn't matter.
    • Admin
      Admin almost 12 years
      @Mat [this] (ideone.com/Ic6KD) is the link at ideone . It's giving same error.
    • ildjarn
      ildjarn almost 12 years
      @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
      ildjarn almost 12 years
      @Mat : That edit is pretty drastic, especially if the OP is convinced they have C code.
    • Mat
      Mat almost 12 years
      @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.)
    • Admin
      Admin almost 12 years
      this is error message i am getting: "52 C:\Users\rachit\Desktop\testprograms\codes\DPTopDown\tets.cp‌​p `count' undeclared (first use this function) "
    • ildjarn
      ildjarn almost 12 years
      @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.)
    • Mat
      Mat almost 12 years
      @user801154: that's not the error that shows in your ideone link.
    • Mat
      Mat almost 12 years
      @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.
    • David Schwartz
      David Schwartz almost 12 years
      @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++.)
    • Admin
      Admin almost 12 years
      @MAT it is same code producing the error i mentioned on my local machine and it's giving different error on ideone.
    • David Schwartz
      David Schwartz almost 12 years
      @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.)
    • ildjarn
      ildjarn almost 12 years
      @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.
    • David Schwartz
      David Schwartz almost 12 years
      @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.
    • Admin
      Admin almost 12 years
      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.
    • Admin
      Admin almost 12 years
      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.
    • CB Bailey
      CB Bailey almost 12 years
      possible duplicate of Using std Namespace
  • Admin
    Admin almost 12 years
    Even if I removed the '#include<algorithm>' line from my code, error persists .
  • jcoder
    jcoder almost 12 years
    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.
  • Nawaz
    Nawaz almost 12 years
    @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.
  • Nicol Bolas
    Nicol Bolas almost 12 years
    @user801154: You are wrong. Here's your original code. Here is your code with the suggested changes. The latter compiles and runs just fine.
  • Admin
    Admin almost 12 years
    picturepush.com/public/8610599 this is link of image producing same error.
  • Admin
    Admin almost 12 years
    @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 .
  • Mat
    Mat almost 12 years
    @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...
  • Benjamin Lindley
    Benjamin Lindley almost 12 years
    @Mat: Very observant. I wonder if that was an honest mistake, or if the OP was trying to pull the wool over our eyes.
  • Admin
    Admin almost 12 years
    @Mat picturepush.com/public/8610702 Saved Code . And yeah that was an honest mistake
  • BitTickler
    BitTickler over 3 years
    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...
  • Victor
    Victor over 3 years
    This answer does not answer the question.
  • Admin
    Admin over 2 years
    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.