Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用命名空间不适用于定义? 我对C++命名空间有点理解困难。考虑下面的例子: //distr.h namespace bogus{ extern const int x; extern const int y; double made_up_distr(unsigned param); }_C++_Namespaces - Fatal编程技术网

使用命名空间不适用于定义? 我对C++命名空间有点理解困难。考虑下面的例子: //distr.h namespace bogus{ extern const int x; extern const int y; double made_up_distr(unsigned param); }

使用命名空间不适用于定义? 我对C++命名空间有点理解困难。考虑下面的例子: //distr.h namespace bogus{ extern const int x; extern const int y; double made_up_distr(unsigned param); },c++,namespaces,C++,Namespaces,现在,如果我在下面定义我的变量,比如cpp,那么一切都可以编译 //distr.cpp #include "distr.h" #include <cmath> const int bogus::x = 10; const int bogus::y = 100; double bogus::made_up_distr(unsigned param){ auto pdf = (exp(param) / bogus::x) + bogus::y; return

现在,如果我在下面定义我的变量,比如cpp,那么一切都可以编译

//distr.cpp

#include "distr.h"
#include <cmath>

const int bogus::x = 10;   
const int bogus::y = 100;

double bogus::made_up_distr(unsigned param){
    auto pdf = (exp(param) / bogus::x) + bogus::y;
    return pdf;
}
我的编译器告诉我,对
x
y
的引用是不明确的。
为什么会这样?

在声明/定义中查找标识符的名称与在使用中查找名称的方式不同。特别是,它不关心使用语句。这有一个非常简单的原因:如果它是不同的,它会导致各种令人不快的惊喜。考虑这一点:

// sneakattack.h
namespace sneakattack { void foo(); }
using namespace sneakattack;

// somefile.cpp
#include "sneakattack.h"
void foo() { std::cout << "Hello\n"; }

// otherfile.cpp
void foo();
int main() { foo(); }
//偷袭.h
命名空间偷袭{void foo();}
使用名称空间偷袭;
//somefile.cpp
#包括“偷袭.h”

void foo(){std::cout这有一个简单的原因,为什么它不能像您期望的那样工作:

namespace bogus {
    const int x;
}
namespace heinous {
    const int x;
}

using namespace bogus;
using namespace heinous;

const int x = 10;
现在,上面的
x
应该指的是
bogus::x
henious::x
还是一个新的全局
::x
? 这将是第三个没有
using
语句的语句,这意味着在这里添加一个using语句将以一种特别微妙的方式改变现有代码的含义

using语句用于为查找引入作用域(通常但不一定是命名空间)的内容

const int x = 10;

通常一开始不需要查找,除非检测ODR冲突。

我认为这是因为在第二种情况下,
x
y
也可能是一个单独的定义,不一定是
bogus::x
bogus::y
。我的意思是编译器认为您需要定义一个不同的
x
,并且<代码> y>代码>同时:<代码>:<代码> >代码>伪::y < /C> >也可见。警告:必须使用Extn来定义C++中的const变量!!!这与C!!!Extn const int b::x=10不同;@ Neelkrk在这种情况下不必要,因为<代码> Extn 从命名空间中的声明继承。(
extern const int x;
const int x = 10;