Click here to Skip to main content
15,892,805 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<iostream>
using namespace std;
struct distance
{ int x; };

int main()
{
	distance d1; // what is wrong in this word (distance), compiler say {IntelliSense: "distance" is ambiguous}
				// i thank this is not recerved key word in c++
		return 0;
}
Posted

Quote:
// i thank this is not recerved key word in c++

Your assumption is right. Hence you have a collision with the STL (with VS 2012 I found distance definition inside the STL's 'xutility internal header'). You may easily verify this either by commenting the using namespace std; or your struct definition (in both cases the ambiguity vanishes).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Feb-13 20:19pm    
5ed, but look how much discussions OP had with me. (I don't really know why didn't I notice your answer 1 hour sooner then mine... :-)
—SA
CPallini 1-Mar-13 3:08am    
Thank you.
How could it be a keyword.

Here is how you could sort it out: 1) rename struct distance; 2) put your cared on the old name distance in Visual Studio, use "Go to definition item" of context menu. You will see that declaration. I did it:

C++
template<class _InIt> inline
    typename iterator_traits<_InIt>::difference_type
        __CLRCALL_OR_CDECL distance(_InIt _First, _InIt _Last)
    {   // return distance between iterators
    typename iterator_traits<_InIt>::difference_type _Off = 0;
    _Distance2(_First, _Last, _Off, _Iter_cat(_First));
    return (_Off);
    }


This is from the file xutility; apparently, you got this include via included iostream.

Problem solved. Next time, you will be able to do it by yourself.

—SA
 
Share this answer
 
Comments
Anderso0on 28-Feb-13 18:32pm    
please give me more explain :) , what is xutility file , and what do this code ?
Sergey Alexandrovich Kryukov 28-Feb-13 19:01pm    
It just does not matter: this is how Microsoft or any other party implemented iostream internally; nothing else to explain.
You should understand the formal approach: there is an ambiguity in some name; OK, now, let's see what's that name; I've demonstrated how. Just repeat my steps, right now or next time; and you will see how it looks...
—SA
Sergey Alexandrovich Kryukov 28-Feb-13 19:05pm    
And, what to do?
How what? remove the clash.

Either 1) remove this include (in this file, I don't see where you need it), 2) remove "using std", and use "std" explicitly where you need it, like std::ifstream, or something; 2) rename your struct::distance. Big deal... :-)

—SA
Anderso0on 28-Feb-13 19:14pm    
:) iwanna read about this , are you have any article benfit to me ?
Sergey Alexandrovich Kryukov 28-Feb-13 19:55pm    
Yes. Look up any C++ manual on these topics: "include" and "namespaces". They are very simple topics...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900