【问题标题】:How to compile the program which uses shared_ptr?如何编译使用 shared_ptr 的程序?
【发布时间】:2014-12-30 22:01:42
【问题描述】:

客户端.h

#ifndef TEST_CLIENT_H_
#define TEST_CLIENT_H_

#include <memory>

class SimpleClient {
public:
  virtual int GetProgress() const = 0;
  virtual char* CutPrefix(char* data) = 0;
  virtual ~SimpleClient() {}
};

std::shared_ptr<SimpleClient> CreateSimpleClient();

#endif  // TEST_CLIENT_H_

客户端.cpp

#include "client.h"

namespace {

class SimpleClientImpl : public SimpleClient {
private:
  int progress_counter_;

public:
  SimpleClientImpl() : progress_counter_(0) {}

  int GetProgress() const;
  char* CutPrefix(char* data);
};

int SimpleClientImpl::GetProgress() const {
  return progress_counter_;
}

char* SimpleClientImpl::CutPrefix(char* data) {
  progress_counter_++;
  return data + *reinterpret_cast<size_t*>(data) + sizeof(size_t);
}

}  // namespace

std::shared_ptr<SimpleClient> CreateSimpleClient() {
  return std::shared_ptr<SimpleClient>(new SimpleClientImpl);
}

我尝试编译:g++ -c client.cpp 并得到以下错误

在 client.cpp:2 中包含的文件中: client.h:13: 错误:预期构造函数、析构函数或在“之前的预期构造函数、析构函数或类型转换

我从各种帖子中了解到我应该与 goose 库链接,但不知道如何使用它。

这是我正在使用的编译器版本:

g++ -v 使用内置规范。 目标:x86_64-redhat-linux 配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable -shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages= c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0- gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux 线程模型:posix gcc 版本 4.4.5 20101112 (Red Hat 4.4.5-2) (GCC)

谁能帮帮我。

【问题讨论】:

  • 您需要将-std=c++11 标志(或更高版本)传递给gcc,假设与gcc 4.4.5 捆绑的libstdc++ 版本甚至包含std::shared_ptr

标签: c++ c++11 gcc


【解决方案1】:

对于这么旧的编译器,您可能应该在编译器标志中添加 -std=c++0x 而不是 -std=c++11。

【讨论】:

    猜你喜欢
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 2020-06-14
    • 2013-10-19
    相关资源
    最近更新 更多