簡體   English   中英

如何解決“命名空間‘std’中的‘mutex’沒有命名類型”?

[英]HOW to solve "'mutex' in namespace 'std' does not name a type"?

我的 C++ 文件如下:

class LogMessage {
 public:
  LogMessage(const char* file, int line)
      :
#ifdef __ANDROID__
        log_stream_(std::cout)
#else
        log_stream_(std::cerr)
#endif
  {
    std::unique_lock<std::mutex> lk(mu_);
    log_stream_ << "[" << pretty_date_.HumanDate() << "] " << file << ":"
                << line << ": ";
  }
  ~LogMessage() { log_stream_ << "\n"; }
  std::ostream& stream() { return log_stream_; }

 protected:
  std::ostream& log_stream_;

 private:
  DateLogger pretty_date_;
  mutable std::mutex mu_;
  LogMessage(const LogMessage&);
  void operator=(const LogMessage&);
};

當我使用g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -pthread -c src/postoffice.cc -o build/postoffice.o在 Ubuntu 上編譯它時g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -pthread -c src/postoffice.cc -o build/postoffice.o ,

我遇到以下問題:

root@4ec615bfc8fa:~/hbsun/nccl/ps-lite# ./make.sh
rm -rf build  tests/test_connection  tests/test_simple_app  tests/test_kv_app_multi_workers  tests/test_kv_app_benchmark  tests/test_kv_app tests/*.d
find src -name "*.pb.[ch]*" -delete
/home/hbsun/nccl/ps-lite/deps/bin/protoc --cpp_out=./src --proto_path=./src src/meta.proto
g++ -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -std=c++11 -MM -MT build/customer.o src/customer.cc >build/customer.d
g++ -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -std=c++11 -MM -MT build/postoffice.o src/postoffice.cc >build/postoffice.d
g++ -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -std=c++11 -MM -MT build/van.o src/van.cc >build/van.d
g++ -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -std=c++11 -MM -MT build/meta.pb.o src/meta.pb.cc >build/meta.pb.d
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include  -pthread -c src/meta.pb.cc -o build/meta.pb.o
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include  -pthread -c src/customer.cc -o build/customer.o
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include  -pthread -c src/van.cc -o build/van.o
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include  -pthread -c src/postoffice.cc -o build/postoffice.o
In file included from ./include/ps/internal/utils.h:6:0,
                 from ./include/ps/base.h:7,
                 from src/van.cc:8:
./include/dmlc/logging.h:189:8: error: 'mutex' in namespace 'std' does not name a type
   std::mutex mu_;
        ^
./include/dmlc/logging.h: In constructor 'dmlc::LogMessage::LogMessage(const char*, int)':
./include/dmlc/logging.h:177:5: error: 'unique_lock' is not a member of 'std'
     std::unique_lock<std::mutex> lk(mu_);
     ^
./include/dmlc/logging.h:177:22: error: 'mutex' is not a member of 'std'
     std::unique_lock<std::mutex> lk(mu_);
                      ^
./include/dmlc/logging.h:177:37: error: 'mu_' was not declared in this scope
     std::unique_lock<std::mutex> lk(mu_);
                                     ^
./include/dmlc/logging.h:177:40: error: 'lk' was not declared in this scope
     std::unique_lock<std::mutex> lk(mu_);
                                        ^
Makefile:52: recipe for target 'build/van.o' failed
make: *** [build/van.o] Error 1
make: *** Waiting for unfinished jobs....
rm src/meta.pb.h
.

似乎我不能使用 std::mutex,我的 g++ 版本是g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 我使用 Ubuntu。

可能最好添加: #include <mutex>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM