Collision name in C ++

When writing code, I ran into this problem:


#include <iostream>

class random { public: random(){ std::cout << "yay!! i am called \n" ;} };

random r1 ;

int main() { std::cout << "entry!!\n" ; static random r2; std::cout << "done!!\n" ; return 0 ; }

When I try to compile this code, I get an error.
error: ârandomâ does not name a type.
When I use a different name for the class, the code works fine.
It seems to be randomdefined somewhere else (although the compiler message is not very informative).

My question is how can I assure that the name I use does not interfere with the name used in the included files. I tried using namespaces, but this leads to ambiguity during the call. Any ideas?
[EDIT]
I used namespaces as using namespace myNSpace
But when I used it as use myNSpace::random, it worked fine.

+3
10

using namespace... , using ... ? :

int main() {
  // ...
  static class random r2; // notice "class" here
  // ...
}

, "class some_class" , , POSIX . : :

  • using namespace foo; - ?

    namespace foo {
    class random
    {
     public:
     random(){ std::cout << "yay!! i am called \n" ;}
    };
    }
    
    int main() {
     using namespace foo; 
     static random r2; // ambiguity!
     return 0 ;
    }
    

    , , , using foo main, . - , . - , -, (foo). .

    , , , - POSIX random foo. ( ), (. man stat , ), .

  • , . , random, main, , random foo, POSIX. ,

    namespace foo {
    class random
    {
     public:
     random(){ std::cout << "yay!! i am called \n" ;}
    };
    }
    
    int main() {
     using foo::random; 
     static random r2; // works!
     return 0 ;
    }
    
+2

namespace myNamespace
{

    class random
    {
    public:
        random(){ std::cout << "yay!! i am called \n" ;}
    };

}

myNamespace::random r1;
+8

POSIX random().

? . , , , man random. (3).

, , .

+5

g++ -ansi. random stdlib.h.

g++ -std=gnu++98, " ISO ++ 1998 GNU". , , . , -ansi .

BSD ( Posix) stdlib GNU. glibc:

http://www.gnu.org/s/libc/manual/html_node/BSD-Random.html

" GNU C, BSD".

, , , . GCC MSVC - , . -ansi gcc , -O2. -fdelete-null-pointer-checks, ​​Linux ). , "" C (, , ++), BSD Posix.

, C , , , , C. , , stdlib BSD, C89. , , -, BSD unixes .

Btw, , random() stdlib.h g++ -E, , , . , "" . "stdlib random" .

+4

, . , .

+2

, :

namespace mydata{
 class random{things};
}

mydata::random;

+2

, . wikipedia

namespace myNamespace
{
     class random
     {
         ....
     };
}
+1

, , . , ( , , litb ), .

:

  • : myNamespace::mySymbol(x);
  • : using myNamespace::mySymbol;

, using myNamespace; , .

:

namespace mns = myNamespace;
mns::mySymbol(x);
+1

cstdlib? , , , .

0

g++, . MinGw 5.1.6 g++ .

0

Source: https://habr.com/ru/post/1724759/


All Articles