【问题标题】:C++ expected primary-expression compile errorC++ 预期的主表达式编译错误
【发布时间】:2012-06-21 15:24:20
【问题描述】:
#include <boost/format.hpp>
#include <boost/scoped_ptr.hpp>
#include <stdexcept>
#include <unordered_map>
#include <functional>
#define DECLARE_OBJECT(classname) namespace {core::declare_object<classname> __declarartion_#classname;}

namespace core {
  class dungeon;
  class object;
  typedef  std::function<object* (dungeon *)> object_creator;
  namespace  library_type {
    enum type {
      landscape = 0, walker, foe, bonus,object = 42
    };
  };
  struct library_error : public std::logic_error 
  {
    explicit library_error(const std::string &what) : std::logic_error(what) {};
  };
  template <enum  library_type::type type>
  class library {
  public:
    static library<type> *instance() {
      if (!m_instance)
        m_instance = new library<type>();
      return m_instance;
    }
    template<typename T>
    void add_object() {
      boost::scoped_ptr<T> obj(T::create(nullptr));
      m_library.insert(obj->name(), T::create);
    }
    const object_creator& get_object(const std::string &key) {
      auto lookup_iterator = m_library.find(key);
      if (lookup_iterator == m_library.end())
        throw library_error(boost::format("Object creator for key `%1%' not found\n") % key);
      return *lookup_iterator;
    }
  private:
    library () {};
    static library<type> *m_instance;
    std::unordered_map<std::string, object_creator> m_library;
  };
  template <enum library_type::type type>
  library<type>*  library<type>::m_instance;
  template <enum  library_type::type type, typename T>
  struct declare_object
  {
    declare_object() {
      auto instance = library<type>::instance();
      auto method = instance->add_object<T>;
      method();
    }
  };
};
int main()
{

}

你好。这个简单的 C++0x 代码在 declare_object 构造函数中给了我错误

example.cpp: In constructor ‘core::declare_object<type, T>::declare_object()’:
example.cpp:52:43: error: expected primary-expression before ‘>’ token
example.cpp:52:44: error: expected primary-expression before ‘;’ token

我真的不知道我错在哪里。也许有清晰的观点和建议? 很抱歉长时间列出。 编辑:答案是auto method = instance -&gt; template add_object&lt;T&gt;;。为什么你删除了你的答案?

【问题讨论】:

  • auto method = instance-&gt;add_object&lt;T&gt;; 缺少括号。可能就是这样。
  • 没有。我想获得指向方法的指针。
  • 哦,把auto换成全名还能用吗?
  • 如果你能指出你的代码中有哪些行,那就太好了。
  • 你的意思是&amp;library&lt;type&gt;::add_object?如果是这样,您将需要(instance-&gt;*method)();

标签: c++ templates boost compilation compiler-errors


【解决方案1】:

要获得指向成员函数的指针,您需要遵循其他答案中的语法。

由于成员函数又是一个模板,所以你需要指出这一点,因为它是一个从属名称:

auto method = &library_type<type>::template add_object<T>;

否则 C++ 会将add_object&lt;T&gt; 中的尖括号解析为小于和大于运算符。

【讨论】:

    【解决方案2】:
      struct declare_object
      {
        declare_object() {
          auto instance = library<type>::instance();
          auto method = &library<type>::template add_object<T>;
          (instance->*method)();
        }
      };
    

    【讨论】:

    • 嘿。花费了血腥的时间来准确确定需要macro in the FAQ 的哪些位。我猜这是他们宏观建议的重点。如果您的代码实际上比粘贴的更复杂,建议您阅读常见问题解答:)
    猜你喜欢
    • 1970-01-01
    • 2011-03-31
    • 2016-06-19
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多