一个c++运行结果

来源:百度知道 编辑:UC知道 时间:2024/06/04 01:10:15
#include<iostream>
using namespace std;
struct tt
{
int hours;
int mins;
};

tt mm(tt t1,tt t2);
tt hh(tt t1,tt t2);

int main()
{
int sum;
tt t1={3,50};
tt t2={1,25};
tt mm(tt t1,tt t2);
cout << sum <<endl;
tt hh(tt t1,tt t2);
cout << sum <<endl;
system("pause");
}

tt mm(tt t1,tt t2)
{ int sum=0;
sum=t1.mins+t2.mins;

}

tt hh(tt t1,tt t2)
{ int sum=0;
sum=t1.hours+t2.hours;

}
我自己写的小程序,结果为2和2 和我想的不一样啊?哪为高手指点以下,小女不胜感激!谢谢!

#include<iostream>
using namespace std;

struct tt
{
int hours;
int mins;
};

tt mm(tt t1,tt t2);
tt hh(tt t1,tt t2);

int main()
{
int sum;
tt t1={3,50};
tt t2={1,25};
tt out1 = mm(t1,t2); //这里传参不需要类型了
cout<<out1.mins<<endl; //cout << sum <<endl; 这里的输出不确定,因为sum变量没有初始化也没为其赋值。
tt out2 = hh(t1,t2);
cout<<out2.hours<<endl; //cout << sum <<endl; 这句也是
system("pause");
return 0;//这里必须返回
}

tt mm(tt t1,tt t2)
{
tt ret; //定义的返回值
ret.mins=t1.mins+t2.mins; //t1的mins和t2的mins求和后赋给返回变量的mins,是这样吧
return ret; //这里必须返回
}

tt hh(tt t1,tt t2)
{
tt ret;
ret.hours=t1.hours+t2.hours; //这儿也是这个意思吧
return ret;//这里必须返回

}

程序是错的,mm,hh函数要有返回值
而且犯了局部变量超出范围使用的错误.mm,hh函数内的sum和main里面的sum不是一个变量<