[英]How do I pass a return value to another function and assign the return value to a variable within that function?
我的程序由 3 个文件组成。 clockType.h - class function 原型,clockTypeImp.cpp - 函数定义,以及测试程序 testClockClass.cpp。
我试图在下面的代码中将第一个 function 的返回值传递给下一个 function。 我想我应该将它作为参考周界或指针*传递。
您可以看到我将返回值分配给变量diffSec
的失败尝试。
(我想要做的是将两个时钟之间的秒差转换为HH:MM:SS
)。
我的clockTypeImp.cpp 实现文件的一部分:
int clockType::clockDiffseconds(clockType otherClock) {
int elapsedSec;
return (abs(elapsedTimeSeconds() - otherClock.elapsedTimeSeconds()));
}
void clockType::secondsToHHMMSS() {
int diffSec = clockType::clockDiffseconds(clockType otherClock);
int diffHours = diffSec/3600;
int diffMinutes = (diffSec/60)%60;
int diffSeconds = diffSec%60;
cout << "Converted to HH:MM:SS: " << diffHours << ":" << diffMinutes << ":" << diffSeconds;
}
我的clockType.h文件的一部分,clockType来自:
class clockType {
public:
void remainingTimeSeconds(int& totalEndSec); // Function to convert clock to time remaining in seconds.
int clockDiffseconds(clockType otherClock); // Function for finding difference in time between clocks.
void secondsToHHMMSS(); // Function for converting seconds to HH:MM:SS.
bool equalTime(const clockType& otherClock) const; // Function to compair the two times.
private:
int hr; // Variable to store the hours.
int min; // Variable to store the minutes.
int sec; // Variable to store the seconds.
};
这是我的测试程序的一部分。
#include <iostream>
#include <iomanip>
#include "clockType.h"
using namespace std;
int main() {
clockType myClock;
clockType yourClock;
int hours;
int minutes;
int seconds;
int elapsedSec;
int totalEndSec;
cout << endl << "myClock Time is not set:\n";
myClock.printTime();
cout << endl << "myClock Time is set:\n";
myClock.setTime(9, 3, 30);
myClock.printTime();
cout << endl;
cout << endl << "yourClock Time is not set:\n";
yourClock.printTime();
cout << endl << "yourClock Time is set:\n";
yourClock.setTime(5, 45, 16);
yourClock.printTime();
cout << endl << endl;
if (myClock.equalTime(yourClock)) {
cout << "Both times are equal." << endl;
} else {
cout << "The two times are not equal." << endl;
}
cout << endl << "Set the time for myClock\nEnter the hours, minutes, and seconds:\n";
yourClock.remainingTimeSeconds(totalEndSec);
cout << "\x1B[32m-->\033[0m" << " The total remaining seconds of \x1B[32myourClock\033[0m is: " << totalEndSec << endl;
myClock.remainingTimeSeconds(totalEndSec);
cout << "\x1B[33m-->\033[0m" << " The total remaining seconds of \x1B[33mmyClock\033[0m is: " << totalEndSec << endl;
cout << endl;
cout << "\x1B[34m-->\033[0m" << " The difference in seconds between the \x1B[34mtwo clocks\033[0m is: " << myClock.clockDiffseconds(yourClock) << endl;
如果您需要查看文件的完整代码,请告诉我。
clockDiffseconds()
是一种非static
方法,它作用this
,计算this
时钟与另一个时钟otherClock
之间的秒差。 那也行。
但是secondsToHHMMSS()
没有otherClock
的概念。 它也是一个仅作用this
的非static
方法。 因此, secondsToHHMMSS()
有意义,您有几个选择:
重新思考secondsToHHMMSS()
对您的实际意义。 按照您编写的方式,它应该按原样打印this
秒数。 在这种情况下,使用clockDiffseconds()
返回的值构造一个新的clockType
object,其elapsedTimeSeconds()
仅返回该值,然后在该新clockType
object 上调用secondsToHHMMSS()
,例如:
class clockType { public:... clockType clockDiff(clockType otherClock) const; void secondsToHHMMSS() const;... };
clockType clockType::clockDiff(clockType otherClock) const { int elaspedSec = abs(elapsedTimeSeconds() - otherClock.elapsedTimeSeconds()); return clockType(elaspedSec); // add a constructor to clockType that stores elaspedSec in such // a way that its elapsedTimeSeconds() can then return it... } void clockType::secondsToHHMMSS() const { int elapsedSec = elapsedTimeSeconds(); int hours = elapsedSec/3600; int minutes = (elapsedSec/60)%60; int seconds = elapsedSec%60; cout << "Converted to HH:MM:SS: " << hours << ":" << minutes << ":" << seconds; }
clockType myClock, yourClock, elapsed;... elapsed = myClock.clockDiff(yourClock); elapsed.secondsToHHMMSS();...
否则,如果您真的希望secondsToHHMMSS()
以秒为单位表示两个时钟之间的差异,那么您必须将otherClock
作为参数传递给secondsToHHMMSS()
,就像您已经对clockDiffseconds()
所做的那样,例如:
class clockType { public:... int clockDiffseconds(clockType otherClock) const; void secondsToHHMMSS(clockType otherClock) const;... };
int clockType::clockDiffseconds(clockType otherClock) const { return abs(elapsedTimeSeconds() - otherClock.elapsedTimeSeconds()); } void clockType::secondsToHHMMSS(clockType otherClock) const { int diffSec = clockDiffseconds(otherClock); int diffHours = diffSec/3600; int diffMinutes = (diffSec/60)%60; int diffSeconds = diffSec%60; cout << "Converted to HH:MM:SS: " << diffHours << ":" << diffMinutes << ":" << diffSeconds; }
clockType myClock, yourClock;... myClock.secondsToHHMMSS(yourClock);...
否则,请考虑将secondsToHHMMSS()
更改为static
方法,然后将所需的秒数作为参数传递,例如从早期调用clockDiffseconds()
,例如:
class clockType { public:... static void secondsToHHMMSS(int seconds);... };
void clockType::secondsToHHMMSS(int seconds) const { int hours = seconds/3600; int minutes = (seconds/60)%60; seconds = seconds%60; cout << "Converted to HH:MM:SS: " << hours << ":" << minutes << ":" << seconds; }
clockType myClock, yourClock;... int diffSec = myClock.clockDiffseconds(yourClock); clockType::secondsToHHMMSS(diffSec);...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.