簡體   English   中英

我如何從主調用插入?

[英]How do i call insert from main?

使用給定的代碼,我如何從 main 調用插入? 我試過了,但我總是遇到錯誤:之前預期的主要表達式。 到底什么是primary-expression?

    Object & front( )
      { return *begin( ); }

    const Object & front( ) const
      { return *begin( ); }

    Object & back( )
      { return *--end( ); }

    const Object & back( ) const
      { return *--end( ); }

    void push_front( const Object & x )
      { insert( begin( ), x ); }

    void push_back( const Object & x )
      { insert( end( ), x ); }

    void pop_front( )
      { erase( begin( ) ); }

    void pop_back( )
      { erase( --end( ) ); }

    // Insert x before itr.
    iterator insert( iterator itr, const Object & x )
    {
        Node *p = itr.current;
        theSize++;
        return iterator( p->prev = p->prev->next = new Node( x, p->prev, p ) );
    }

從 main 調用函數:

void My_Function()
{
  std::cout << "My_Function\n"
}

int main()
{
  My_Function();
  return 0;
}

從 main 調用對象的方法:

class Object
{
  public:
    void print() { std::cout << "Object\n";}
};

int main()
{
  Object o;
  o.print();
  return 0;
}

您應該能夠在一些優秀的 C++ 教科書中找到這些示例。

編輯 1:靜態對象函數

您還可以在對象內將方法聲明為static方法:

class Object_With_Static
{
  public:
    static void Print_Name()
    {
       std::cout << "Object_With_Static\n";
    };
};

int main()
{
  Object_With_Static::Print_Name();
  return 0;
}

暫無
暫無

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

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