简体   繁体   English

如何将返回值传递给另一个 function 并将返回值分配给该 function 内的变量?

[英]How do I pass a return value to another function and assign the return value to a variable within that function?

My program consist of 3 files.我的程序由 3 个文件组成。 The clockType.h - the class function prototypes, clockTypeImp.cpp - definitions of functions, and the testing program testClockClass.cpp. clockType.h - class function 原型,clockTypeImp.cpp - 函数定义,以及测试程序 testClockClass.cpp。

I am trying to pass the return value of the first function to the next function in the code below.我试图在下面的代码中将第一个 function 的返回值传递给下一个 function。 I think I am supposed to pass it as a reference perimeter, or a pointer*.我想我应该将它作为参考周界或指针*传递。

You can see my failed attempt at assigning the return value to the variable diffSec .您可以看到我将返回值分配给变量diffSec的失败尝试。

(What I am trying to do is convert the difference in seconds between two clocks and out put that difference as HH:MM:SS ). (我想要做的是将两个时钟之间的秒差转换为HH:MM:SS )。

Part of my clockTypeImp.cpp implementation file:我的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;
}

Part of my clockType.h file where clockType is coming from:我的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.
};

Here is part of my testing program.这是我的测试程序的一部分。

#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;

Let me know if you need to see the full code of the file(s).如果您需要查看文件的完整代码,请告诉我。

clockDiffseconds() is a non- static method that acts on this , calculating the difference in seconds between this and another clock otherClock . clockDiffseconds()是一种非static方法,它作用this ,计算this时钟与另一个时钟otherClock之间的秒差。 That is fine.那也行。

But secondsToHHMMSS() has no concept of otherClock .但是secondsToHHMMSS()没有otherClock的概念。 It is also a non- static method that acts only on this .它也是一个仅作用this的非static方法。 So, for secondsToHHMMSS() to be meaningful, you have a few choices:因此, secondsToHHMMSS()有意义,您有几个选择:

  • re-think what secondsToHHMMSS() actually means to you.重新思考secondsToHHMMSS()对您的实际意义。 The way you have written it, it should just print the current seconds of this as-is.按照您编写的方式,它应该按原样打印this秒数。 In which case, use the value returned by clockDiffseconds() to constructs a new clockType object whose elapsedTimeSeconds() returns just that value, and then call secondsToHHMMSS() on that new clockType object, eg:在这种情况下,使用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();...
  • Otherwise, if you really want secondsToHHMMSS() to express the difference in seconds between two clocks, then you will have to pass in the otherClock as a parameter to secondsToHHMMSS() , just like you already do with clockDiffseconds() , eg:否则,如果您真的希望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);...
  • Otherwise, consider changing secondsToHHMMSS() to be a static method instead, and then pass in the desired seconds as a parameter, such as from an earlier call to clockDiffseconds() , eg:否则,请考虑将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.

相关问题 使用 args 传递函数并将值返回给另一个函数 - Pass function with args and return value to another function c++ - 如何将函数的返回值分配给变量? - How to assign return value of a function into a variable in c++? 如何将值返回到另一个函数 - How to return a value to another function 如何按值返回“自动”返回类型函数 - How do I return by value for an “auto” return type function 如何在另一个成员函数中用常量返回类型修改成员函数的返回值? - How to modify the return value of a member function with constant return type within another member function? 为什么我可以将返回const T&的函数的返回值分配给T变量,而不分配给T变量? - Why can I assign the return value of a function that returns `const T&`, to a `T` variable but not to a `T&` variable? 如何在不使用临时变量的情况下取消引用返回指针的函数的返回值? - How do I dereference the return value of a function returning a pointer without using a temporary variable? 将 Function 返回值存储到变量中 - Store Function Return Value into a Variable 如何使用const_reference类型声明一个变量并赋值给它,front()函数的返回值 - How to use const_reference type to declare a variable and assign to it, the return value of front() function 如何从线程函数返回值? - How do I return a value from thread function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM