c++ - 我如何从主调用插入?
问题描述
使用给定的代码,我如何从 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 ) );
}
解决方案
从 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;
}
推荐阅读
- python - Django - 在创建或更新查询中使用模型字段部分
- webpack - 对本地文件的代理请求
- javascript - 样式化自定义元素:字体样式
- ios - 月中的星期几(数字)。该示例适用于 4 月的第一个星期三
- javascript - 在 FullCalendar 中处理新行
- flutter - 滚动 TabBarView 子项时使 SliverAppBar 可滚动
- python - 在 Python 中使用 teradatasql 模块连接到 Teradata
- c# - 如何获取在 Xamarin MacOS 中以编程方式创建的 NSTextField 的值?
- deep-learning - 如何处理我的数据集中不相关的图像
- javascript - 如何在角度中删除连字符前的单词