堆栈中大小变量输入错误 (C++)
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;
}
}
在 Create 函数中,当编译器尝试接受输入时,它会显示错误"错误:与'运算符>>不匹配'(操作数类型为'std::istream {aka std::basic_istream}' 和 'int*'("。该错误是无法理解的。任何人都可以帮助解决问题吗?
&st->size
是一个int*
。您无法从cin
读入int*
.由于您只需要读取大小,因此删除&
应该可以修复它:
cin >> st->size;
您在display
中的for
循环也不正确。您需要执行以下操作:
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];
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'