[英]Why do I get the compiler error “does not name a type” when my code is outside of a function scope?
當我偶然發現這個我不理解的錯誤時,我正在通過在隨機位置生成對象來測試我正在構建的游戲引擎。
“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);
}
“主.cpp”:
#include <iostream>
#include "foo.h"
int main() {
// Actually run the program
}
嘗試編譯此代碼會給我錯誤消息:
error: 'r_gen' does not name a type
r_gen.seed(r_seed);
^~~~~
我將 Eclipse 與 MinGW 一起使用。 我不確定它為什么將r_gen
解釋為一種類型。 此外,將上述代碼包裝在 function (命名空間foo
中的所有內容)中可以使其正確編譯。
我有一個理論問題和一個實用問題:
只需更改前兩個定義的順序,並從種子構造生成器:
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.