Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 'this_thread': the symbol to the left of a '::' must be a type?

My headers:

#include <chrono>
#include <thread>
#include <iostream>
#include <string>
#include <fstream>
#include "include\curses.h"

My code problem:

        std::this_thread::sleep_for(std::chrono::milliseconds(500));

My error:

error C3083: 'this_thread': the symbol to the left of a '::' must be a type

It just makes no sense to me!? Every header needed is added. Intellisense (I'm on VS2012) detects the references and parameters. It just won't compile.

Other related errors

*error C2039: 'sleep_for' : is not a member of 'std'*

*error C3861: 'sleep_for': identifier not found*

like image 478
orlando2bjr Avatar asked Feb 16 '23 15:02

orlando2bjr


2 Answers

I just experienced a similar issue - VS Intellisense had no problems with my code, but I was getting the same error you had when building, not just in VS but in GCC as well. (not a VS bug)

One big issue I had is that 2 headers were including each other. Essentially, because one file includes the other but the other is dependent on the first, you can get some strange behavior.

As you are no doubt aware, headers normally have #ifndef's at the top which encompass the entire contents of the header, so it can never be included more than once. This at least saves us from getting caught in an endless include loop, though we still have a problem.

When the second is included, it is referencing the first, but the first is already defined, so the first does not get included into the second, then the code from the second header is copied into the first where it was included. Because the second header is included above the functions it needs, you will get errors about things not being defined, and even namespaces not existing (because these are not defined until after the second is inserted at the top of the first.)

Here are 2 solutions:

  1. In my case, because I was only dependent on the first file with a single small function, I chose to simply remove one of the dependencies by manually inlining the code in the second.

  2. You should also be able to add prototypes to the functions you want to use, so if one gets included below the other you at least have a prototype which tells the compiler that "this will be defined later, just trust me it exists."

The way I found out what was going on was by moving the problem header above all the others in the include list, which revealed more errors many of which were similar but now plaguing the other include.

I'm not totally sure why the using namespace worked for you, but all I can figure is that this using statement at least tells the compiler to treat the name as a namespace rather than some undefined thing, hence the error about your namespace not being a type.

like image 121
PolyMesh Avatar answered Feb 19 '23 18:02

PolyMesh


Thanks to @stefan comments I could solve the problem. This is probably a VISUAL STUDIO 2012 compilling bug I'll put a short version of the answer to keep things clean, but I must add that the #include "include\curses.h" was not na issue (though I'll follow Jonathan's tips about the / ;-))

REQUIRED Headers:

#include <chrono>
#include <thread>

REQUIRED namespace (It won't compile with just std::this_thread::sleep_for() on VS2012:

using std::this_thread;

Fixed code line:

sleep_for(std::chrono::milliseconds(500));

Extra Notes Using g++ compiler (with -std=C++11 option) on Linux this problem doesn't occur. I.e. You can simply use:

std::this_thread::sleep_for(std::chrono::miliseconds(500));

Without using std::this_thread declaration

If anyone else can explain this further (on VS2012), please add a comment!

like image 36
orlando2bjr Avatar answered Feb 19 '23 17:02

orlando2bjr