rand()和命名空间

时间:2013-04-02 18:51:46

标签: c++ random namespaces

我正在通过引入名称空间来修改给我的一些代码的特定结构。 我不知道为什么作者做出了特定的设计选择,但我试图填充的东西看起来像这样:

myClass.h:

#ifndef MYCLASS_H
#define MYCLASS_H

#include myHeader.h

// namespace myNamespace {   // --- me introducing namespace   
template<class T>
class MyClass : public Class<T> {
...
}
....
#include myClass_inline.h
// }   // --- me closing namespace
#endif

myClass_inline.h:

template<class T>
MyClass<T> my_rand(...) {
   srand(...); //  (1) no problem with this
   ...
   a(rand()%...)=double(::rand()*....); // (2) problematic line with rand's  
}
写道的方式一切都没有问题。 stdlib.h所需的rand()位于myHeader.h

如果/当我引入命名空间时,编译器会在第(2)行生成和错误:

myClass_inline.h: In function 'myNamespace::MyClass<T> myNamespace::my_rand()':
myClass_inline.h:12:12: error: no matching function for call to 'rand()'
myClass_inline.h:12:12: note: candidate is:

有趣的是,srand在同一个函数中没有问题。 有人可以告诉我我做错了什么吗? 坦率地说,我从来没有在当前的一端通过另一个标题包含函数。所以我不确定我是否正确引入namespace

我不知道的另一件事是rand()::rand()之间有什么区别?这个空命名空间意味着什么?

如果我将::rand替换为rand,它将无效。所以我很困惑......

EDIT1

如下面评论中所述,我忘记了重要信息。 首先,12:12指向第一个rand,即rand()

其次,确实在myHeader.h

中包含的标题中定义了一些其他函数rand()
AnotherHeader.h:339:16: note: template<class T> myNamespace::AnotherClass<T> myNamespace::rand(unsigned, unsigned, int)
AnotherHeader.h:339:16: note:   template argument deduction/substitution failed:
...
myClass_inline.h:12:12: note:   candidate expects 3 arguments, 0 provided
抱歉,我没有阅读编译器错误的经验...... 所以,在我尝试引入命名空间之前,现在我不确定哪个rand()打算调用(确实被调用)。可能我需要更多地研究代码......

EDIT2

rand()替换第一个::rand(),整个事情用我的命名空间编译。非常感谢 ! 尽管如此,我不明白编译器如何将它与另一个具有不同输入参数的rand()混合...

0 个答案:

没有答案