Foo Boy
Foo Boy

Reputation: 5

Compiler doesn't see the C++11 identifiers

When I was trying to build the Google test c++ project I caught the errors

Error   C3861   't1': identifier not found
Error   C2065   't1': undeclared identifier
Error   C2039   'thread': is not a member of 'std'
Error   C2065   'thread': undeclared identifier
Error   C2146   syntax error: missing ';' before identifier 't1'

My test code is:

#include <future>
#include <thread>

#include "pch.h"
TEST(...)
{
    // preconditions here

    std::thread t1([&] {
        Sleep(100);
        testee.enqueue(item);
        });
    
    t1.join();
    
    // other logic
}

Why I cannot use the std::thread and other C++11 features in my project? How can I do it?

Upvotes: 0

Views: 407

Answers (3)

Yujian Yao - MSFT
Yujian Yao - MSFT

Reputation: 954

Regarding pch.h, you could refer to this issue. I tested the code you uploaded in VS2019, and there was no error in the code. The result is shown in the picture.

#include <iostream>
#include <future>
#include <thread>
#include<Windows.h>
int main()
{
    std::thread t1([&] {
        Sleep(1000);
        });

    t1.join();
} 

enter image description here

Regarding the use of C++11, you could set it in the project properties>configuration properties>general>C++ Languae Standard. I suggest you use C++14, because C++14 already contains the features of C++11.

enter image description here

Upvotes: 0

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38773

#include "pch.h" must be first. Other #include directive prior #include "pch.h" are ignored.

Upvotes: 2

eerorika
eerorika

Reputation: 238401

Why I cannot use the std::thread and other C++11 features in my project?

Potential reason 1: You cannot use C++11 features unless you use C++11 or a later standard.

Potential reason 2: You cannot use std::thread unless you include the header that defines std::thread. It's defined in the header <thread>.

Upvotes: 0

Related Questions