[英]Function returns a wrong number C++
我有问题。 在函数 "pop" 内部,int rezultat 具有正确的值,但在外部,它是 0。我不会 C++,但我必须为学校做这件事,我花了 2 个小时,我不知道我做错了什么... 请帮忙。
#include <iostream>
int rezultat;
struct stosik {
int x;
stosik *next;
};
void push(stosik* &stos, int x) {
stosik* tymczasowy = new stosik;
tymczasowy->x = x;
tymczasowy->next = stos;
stos=tymczasowy;
delete tymczasowy;
}
bool isEmpty(stosik* stos){
return stos != NULL;
}
//HERE@@@@@@@@@@@@@@@@@@@@@@@
bool pop(stosik* &stos, int rezultat){
if (!isEmpty(stos)) {
return false;
}
stosik* tymczasowy = stos;
rezultat = tymczasowy -> x;
if(stos->next !=NULL){
tymczasowy = stos -> next;
stos=tymczasowy;
}
else {
std::cout << "Nic" << std::endl;
}
// delete tymczasowy;
std::cout <<"Rezultat na koncu dziala funkcji "<<rezultat << std::endl;
return true;
}
bool topEl(stosik* &stos, int* result, int mekeke){
if (isEmpty(stos))
{
return false;
}
*result = stos ->x;
mekeke=*result;
return true;
}
int main(){
stosik* stos_roboczy=NULL; // deklaracja stosu, domyślnie NULL
std::cout << "0 empty, 1 something "<< std::endl;
std::cout << isEmpty(stos_roboczy) << std::endl;
std::cout << "" << std::endl;
push(stos_roboczy, 5);
push(stos_roboczy, 15);
push(stos_roboczy, 25);
std::cout << "0 empty, 1 something<<std::endl;
std::cout << isEmpty(stos_roboczy) << std::endl;
std::cout << "" << std::endl;
//AND HERE @@@@@@@@@@@@@@@@@@@@@@@
pop(stos_roboczy, rezultat);
std::cout <<"Wrong result after function: "<<rezultat << std::endl;
//
// pop(stos_roboczy, rezultat);
// pop(stos_roboczy, rezultat);
//
// std::cout << rezultat << std::endl;
}
您有两个不同的整数,它们碰巧具有相同的名称:在文件顶部声明的全局rezultat
,以及函数pop()
的参数列表中的函数参数rezultat
。
您将全局rezultat
的值传递给pop()
,然后为函数内部的函数参数rezultat
分配一个新值。 当函数退出时,函数参数rezultat
消失了。 您没有为全局rezultat
分配新值,因此它具有与之前相同的值。
如果要将变量的值从函数内部发送到外部,请使用return
语句。
如果您将全局重命名为其他名称,则应该可以消除您对两者之间的混淆。
另外,请参阅@ForeverStudent 的优秀回答。 他发现了许多其他问题,您需要查看这些问题。
你的代码有很多问题。
首先,您的push
函数具有签名void push(stosik* &stos, int x)
根据您的语义,您最有可能想要void push(stosik* stos, int x)
你在pop
的签名中有同样的问题
同样在push
的身体内,你有:
stosik* tymczasowy = new stosik; //allocate on heap, OK
tymczasowy->x = x; //OK
tymczasowy->next = stos;//OK
stos=tymczasowy; //BOTH pointers now point to the same memory
delete tymczasowy;//you are freeing memory for both.
//stos->next is now inaccessible and leaked
最后一行造成内存泄漏,两个指针都指向同一个位置,您正在释放内存。 确保您仍然拥有指针,但它不再指向有效的对象实例。
此外,您正在使用将初始化为 0 的外部全局变量rezultat
。
现在你的pop
函数有一个同名的形参。 这导致rezultat
引用函数作用域中的变量,而不是全局变量。
我建议你避免使用全局变量,但如果你必须使用它们,至少不要在块范围内覆盖它们。
bool pop(stosik* stos, int rezultat) //valid code, but terrible practice
{
//here rezultat refers to the variable passed in NOT the global
//global variable is still accessible in this scope via ::rezultat
if (!isEmpty(stos))
{
return false;
}
stosik* tymczasowy = stos;
rezultat = tymczasowy -> x;
//this rezultat will be destroyed when this function returns.
...
}
如果您决定顽皮并使用全局变量,那么您需要这样做:
bool pop(stosik* stos)
{
if (!isEmpty(stos))
{
return false;
}
stosik* tymczasowy = stos;
rezultat = tymczasowy -> x;
//now this refers to the global variable
...
}
当你想通过一个参数返回一个值时,你需要使用一个引用,例如
bool pop(stosik* &stos, int &rezultat){
注意rezultat
之前的&
。 当你调用pop(stos_roboczy, rezultat);
,这会将rezultat
的引用放在堆栈上,而不是其值。 并且它允许函数将给定值返回给调用者的变量,而不仅仅是修改局部变量/参数。
有关更多详细信息,另请参阅通过引用传递参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.