[英]C++ error with INPUT class (Stack around the variable was corrupted.)
[英]input to size variable error in stack (c++)
#include <iostream>
using namespace std;
struct stack{
int size;
int top;
int *s;
};
void create(struct stack *st){
cout<<"enter size :";
cin>>(&st->size); ///This line poses error when i run the program
st->top=-1;
st->s=new int[st->size*sizeof(int)];
}
void display(struct stack st)
{
for(int i:(st->s)){
cout<<i;
}
}
在创建 function 时,当编译器尝试输入时,它显示错误“错误:'operator>>' 不匹配(操作数类型是'std::istream {aka std::basic_istream}'和'int*' )”。 错误是无法理解的。 任何人都可以帮助解决这个问题吗?
&st->size
是一个int*
。 您无法将cin
读入int*
。 由于您只需要读取大小,因此删除&
应该修复它:
cin >> st->size;
您for
display
中的循环也不正确。 您需要执行以下操作:
for(int i = 0; i < size; ++i)
cout << st->s[i];
另外,这一行:
st->s=new int[st->size*sizeof(int)];
似乎很奇怪。 如果你想在数组中有st->size
元素,那么你只需要:
st->s = new int[st->size];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.