【发布时间】:2019-07-03 18:52:37
【问题描述】:
我无法理解为什么我的代码会为我提供一些虚拟值。
有人可以帮我找出我的错在哪里,原因是什么?
#include <stdio.h>
#include <iostream>
class mypair
{
public:
int a,b;
public:
int print (int first , int second)
{
a = first;
b = second;
std::cout<<a <<" hello "<<b;
}
int getmax();
};
int mypair ::getmax()
{
int res;
res = (a>b)?a:b;
std::cout<<res;
return res;
}
int main ()
{
mypair abc;
std::cout<<abc.print(5,6);
std::cout<<abc.getmax();
}
【问题讨论】:
-
你应该打开你的编译器警告。它会告诉您,您不会从
mypair::print()返回任何东西,但您应该返回int。 -
5 你好 6629564866
-
除了答案,您将打印最大数量两次。尝试自己调试它:)