简体   繁体   English

当我的代码在 function scope 之外时,为什么会出现编译器错误“未命名类型”?

[英]Why do I get the compiler error “does not name a type” when my code is outside of a function scope?

I was testing a game engine I am building by generating objects at random positions when I stumbled across this error that I do not understand.当我偶然发现这个我不理解的错误时,我正在通过在随机位置生成对象来测试我正在构建的游戏引擎。

"foo.h": “foo.h”:

#include <random>
#include <chrono>
#include <functional>

namespace foo {

std::default_random_engine r_gen;
auto r_seed = std::chrono::system_clock::now().time_since_epoch().count();

r_gen.seed(r_seed);  // This is the line giving an error

std::uniform_real_distribution<float> r_dist(-1.0, 1.0);
auto r_float = std::bind(r_dist, r_gen);

}

"main.cpp": “主.cpp”:

#include <iostream>
#include "foo.h"

int main() {

    // Actually run the program

}

Attempting to compile this code gives me the error message:尝试编译此代码会给我错误消息:

error: 'r_gen' does not name a type
r_gen.seed(r_seed);
^~~~~

I am using Eclipse with MinGW.我将 Eclipse 与 MinGW 一起使用。 I'm not sure why it is interpreting r_gen as a type.我不确定它为什么将r_gen解释为一种类型。 And furthermore, wrapping the above code in a function (everything inside namespace foo ) allows it to compile correctly.此外,将上述代码包装在 function (命名空间foo中的所有内容)中可以使其正确编译。

I have a theory question and a pragmatic question:我有一个理论问题和一个实用问题:

  • (Theory) Why does my example code not compile? (理论)为什么我的示例代码无法编译?
  • (Pragmatic) How should I be arranging this code so that it only seeds the generator once? (务实)我应该如何安排这段代码,以便它只为生成器播种一次?

Just change the order of the first two definitions, and construct the generator from the seed:只需更改前两个定义的顺序,并从种子构造生成器:

auto r_seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine r_gen(seed);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c++ 为什么会出现“未命名类型”错误? - c++ Why do I get “does not name a type” error? 为什么我的代码会抛出“未命名类型错误”? - Why does my code throw “does not name a type error”? 为什么我会收到此错误? “[变量]没有命名类型”在 C++ 中用于 std::string 数组,即使它已包含在同一个 scope 中 - Why do I get this error? “[variable] does not name a type” in C++ for std::string array even though it has been included and is in the same scope 为什么我得到错误“&#39;cout&#39;在命名空间&#39;std&#39;中没有命名类型”当我使用“使用cout = std :: cout;”? - Why do I get error “'cout' in namespace 'std' does not name a type” when I use “using cout = std::cout;”? 为什么我会得到“错误” <class> 这个简单的C ++程序中没有命名类型错误? - Why do I get “error <class> does not name a type” error in this simple C++ program? 为什么在访问 `extern` 定义的变量时会出现“未命名类型” - Why do I get "does not name a type" when I access an `extern` defined variable 编译器错误:未命名类型 - Compiler error: does not name a type 如果我将函数命名为`swap`,为什么会出现模板错误,但是`Swap`是否合适? - Why do I get a template error if I name my function `swap`, but `Swap` is okay? 为什么在代码编译正常时会出现“类型为“LPCSTR”的参数与类型为“LPCWSTR”的参数不兼容的错误? - Why do I get "argument of type "LPCSTR" is incompatible with parameter of type "LPCWSTR" error when code compiles fine? 不知道为什么我收到“错误:没有命名类型” - Not sure why i get the "error: does not name a type "
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM