簡體   English   中英

如何將返回值傳遞給另一個 function 並將返回值分配給該 function 內的變量?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM