1

I didn't understand why i take this "strange" error. I read similar questions but it didn't answer my questions. If i define the array inside main function rather than global scope, there is no error. But assume that i have to define this array in global scope. Why do i take this error? Here is the code :

#include <iostream>
#include <cstring>

using namespace std;

int right[1005];
int main()
{
    memset(right,0,sizeof(right));
return 0;
}

Here is the error :

memset2.cpp: In function ‘int main()’:
memset2.cpp:9:9: error: reference to ‘right’ is ambiguous
  memset(right,0,sizeof(right));
     ^
memset2.cpp:6:5: note: candidates are: int right [1005]
 int right[1005];
     ^
In file included from /usr/include/c++/4.8/ios:42:0,
             from /usr/include/c++/4.8/ostream:38,
             from /usr/include/c++/4.8/iostream:39,
             from memset2.cpp:1:
/usr/include/c++/4.8/bits/ios_base.h:924:3: note:                 std::ios_base& std::right(std::ios_base&)
   right(ios_base& __base)
   ^
memset2.cpp:9:24: error: reference to ‘right’ is ambiguous
  memset(right,0,sizeof(right));
                    ^
memset2.cpp:6:5: note: candidates are: int right [1005]
 int right[1005];
     ^
In file included from /usr/include/c++/4.8/ios:42:0,
             from /usr/include/c++/4.8/ostream:38,
             from /usr/include/c++/4.8/iostream:39,
             from memset2.cpp:1:
/usr/include/c++/4.8/bits/ios_base.h:924:3: note:                 std::ios_base& std::right(std::ios_base&)
   right(ios_base& __base)
   ^
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
metis
  • 1,024
  • 2
  • 10
  • 26
  • I am used to write all using namespace in my codes. I am really surprised now. Why you advised me to not use it? Can you guide me? – metis May 08 '15 at 20:30
  • 2
    no problem in using it, but you must be sure that you never use user defined function, object or variable which has same name of C++ standard library. I think `using namespace std ` is an old style. – youssef May 08 '15 at 20:33
  • for example; you can say `#include ` then use the cout standard object like this `std::cout << "somthing" ` – youssef May 08 '15 at 20:35
  • In what other things should i need to add "std::"? Since i always write "using namespace std;" , i don't know – metis May 08 '15 at 20:37
  • 1
    one more thing is helpful. In case of header files it is a bad practice to use `using namespace std` – youssef May 08 '15 at 20:37
  • before any standard functions, objects or variables. – youssef May 08 '15 at 20:40
  • for initial learning having a `using namespace std` is fine. Just be aware that in general you do not want to do that as it obfuscates what you are really doing and it can even cause some problems when you accidentally use the same name as something that already exists in std:: - like is the source of your problem this time. – ABaumstumpf Aug 20 '23 at 09:54

3 Answers3

7

Namespace std has already name right and you included names form std in the global namespace by means of directive

using namespace std;

So to avoid the ambiguity use a qualified name

memset( ::right, 0, sizeof( ::right ) );

Or remove the directive and in this case you may use unqualified name right because the compiler will seek the name only in the global namespace.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

remove using namespace std ; from your code and precede any standard function or object with std::

youssef
  • 613
  • 4
  • 16
-1

If following statement has ambiguous error

 memset ( (char*)&mybuf, 0, sizeof(mybuf) );

give this a go by preceeding memset

std::memset ( (char*)&mybuf, 0, sizeof(mybuf) );
                                     
toyota Supra
  • 3,181
  • 4
  • 15
  • 19