如何从主调用插入?

How do i call insert from main?

本文关键字:插入 调用      更新时间:2023-10-16

使用给定的代码,如何从主调用插入? 我试过了,但我总是收到错误:之前的主要表达式。 初级表达式到底是什么?

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_Functionn"
}
int main()
{
My_Function();
return 0;
}

从 main 调用对象的方法:

class Object
{
public:
void print() { std::cout << "Objectn";}
};
int main()
{
Object o;
o.print();
return 0;
}

你应该能够在一些好的C++教科书中找到这些例子。

编辑 1:静态对象函数

您还可以将方法声明为对象内部的static

class Object_With_Static
{
public:
static void Print_Name()
{
std::cout << "Object_With_Staticn";
};
};
int main()
{
Object_With_Static::Print_Name();
return 0;
}