簡體   English   中英

為什么此C ++模板會產生錯誤,要求缺少分號?

[英]Why does this C++ template produce an error asking for the missing semicolon?

我只是從Bjorn Karlsson撰寫的“超越標准庫”中輸入一些解釋活頁夾工作原理的代碼。 我正在使用Visual Studio 2010 Express,並且遇到缺少的半冒號錯誤。

這一點似乎很好:

// template for a simple function object
/////////////////////////////////////////////////////////////////////////////
template<typename R, typename T, typename Arg>
  class simple_bind_t {
    typedef R (T::*fn) (Arg);  // fn defines a type - a ptr to member fn
                               // of object T, returning an R, and taking Arg

    fn _fn;                    // the function object owns a pointer to function.
    T  _t;                     // it also owns an object of type T.

  public:

    simple_bind_t(fn f, const T& t):
             _fn(fn), _t(t) {} // instantiate mber variables

    R operator()(Arg& a) 
    { 
      return (_t->_fn)(a);    // invoke the member function on t, pass a as an arg.
    }

  };

這樣就可以了。 但是,當我定義創建函數對象的實際函數時(請參見下面的代碼片段),在行R (T::*fn)(Arg),R下方有一條紅線。 當我將鼠標懸停在它上面時- Error: expected a ';' )。

template <class R, class T, class Arg> 
   simple_bind_t<R,T,Arg> simple_bind (
     R (T::*fn)(Arg),                     
     const T& t,
     const placeholder&)
   { return simple_bind_t<R,T,Arg>(fn,t); } // construct an object of simple_bind_t<R,T,Arg>
                                        // pass it pointer to the member function, fn
                                        // and the object of type T to bind to, t

誰能發現語法錯誤在哪里?

您在這里打錯了:

simple_bind_t(fn f, const T& t):
         _fn(fn), _t(t) {} // instantiate mber variables
// ----------^^

fn命名一個類型 ,您打算使用參數f 可能還有其他錯誤,但是由於您沒有發布足夠完整的代碼(例如,缺少placeholder的定義),因此無法找到它們。

暫無
暫無

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

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