Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
414 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...