首页 > 解决方案 > 我如何从主调用插入?

问题描述

使用给定的代码,我如何从 main 调用 insert?我试过了,但我总是收到错误:expected 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 ) );
    }

标签: c++

解决方案


从 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;
}

推荐阅读