65,206
社区成员
发帖
与我相关
我的任务
分享
void output(const stack<int>& s)
{
cout << s.top() << endl;
}
int main()
{
stack<int> s;
s.push(1);
output(s);
return 0;
}
你把stack头文件里面的const T& top(void) const;函数注释掉看看,你能给我编译过?
对const的对象只能调用const函数,这是基本常识!!!
stack<int> nonConstStack;
stack stack<int> nonConstStack;
//调用这个函数:T& top(void);
int& value = nonConstStack.top();
return 0;
const stack<int> constStack;
//调用这个函数:const T& top(void) const,如果只定义了T& top(void),会导致编译错误。
const int& value = constStack.top();