调用函数只有在函数开头有cout语句时才会给出结果
Calling a function gives result only if it has a cout statement in the beginning
我试图在c++中实现一个简单的堆栈。除了这个函数minm()之外,我的代码工作得很好。我得到了意想不到的结果。如果我注释掉粗体的行,那么我的过程甚至不会运行。此外,您可以看到我的函数minm() &Minmm()属于粗体行。请帮帮我。
解释如下:
Functions minm() & &cout<<"called"<<
#include<iostream>
using namespace std;
const int MAX_ALLOWED_SIZE = 100000000;
class MyStack{
int *a;
int *min, *max;
int top;
int size;
public:
MyStack(int s=MAX_ALLOWED_SIZE);
void push(int i);
int pop();
int maxm();
int minm() // This function is failing
{
//cout<<"calledt";
if(!stackEmpty())
{
cout<<min[top]<<" = " <<a[min[top]]<<endl;
return a[min[top]];
}
return NULL;
}
int minmm() // This function is working
{
cout<<"calledt";
if(!stackEmpty())
{
cout<<min[top]<<" = " <<a[min[top]]<<endl;
return a[min[top]];
}
return NULL;
}
bool stackEmpty();
void printStack();
};
int main()
{
MyStack s;
int t;
while(true)
{
scanf("%d",&t);
s.push(t);
cout<<"min = "<<s.minm()<<endl;
cout<<"min = "<<s.minmm()<<endl;
if(t==-1) break;
}
}
输入:234
23
-1
输出:min = 0
called a[0] = 234
min = 234
min = 0
called a[1] = 23
min = 23
min = 0
called a[2] = -1
min = -1
现在,我从ideone上的两个函数得到相同(错误)的结果,而函数minmm()在我的系统上返回(我在Code::Blocks 12.11中使用GNU GCC编译器)。
在你的stackEmpty中,你没有返回false:
bool stackEmpty(){
if(top == -1) return true;
return false;
}
或
bool stackEmpty(){
return (top == -1);
}
相关文章:
- 如何从递归函数中完全返回,该函数给出了每个函数结果的累积相加?
- 分段错误分配函数结果
- 使用函数结果作为参数的 C++
- 从可调用可变参数元组中的函数结果创建元组
- 将多个向量(函数结果)组合到一个使用模板中
- 是否可以使用自动占位符来推断非类型模板参数中的函数结果
- 大小由虚函数结果决定的数组
- C/C++编译器是否会通过重用最近计算的函数结果来优化代码?
- 从函数结果中分配 std::shared_ptr 的一行是什么?
- 将函数结果分配给变量的问题
- std::具有自定义比较函数结果的排序函数错误:必须调用对非静态成员函数的引用
- C++SFINAE运算符/函数结果类型检查
- 将函数结果作为"output parameter"返回是什么意思?
- 这将是在将函数分配给变量或一次又一次地调用函数之间使用函数结果的最佳方式
- C++中的绑定函数结果
- 有没有比生成/累积更快的方法来计算函数结果的平均值
- 为什么当递归函数结果相乘时,g++ 仍然优化尾递归
- C++成员函数结果缓存实现
- 用于输出列中对数函数结果的合适循环(c++)
- 将记录作为函数结果从Delphi DLL传递到c++