0

割り当てに特定の制限があるため、このコードでは多くのことを変更できません。コードは次のとおりです。

#include <iostream>
#include<cstdlib>
#include<cstring>

using namespace std;
struct MyTime { int hours, minutes, seconds; };
int DetermineElapsedTime(const MyTime *t1, const MyTime *t2);
const int hourSeconds = 3600;
const int minSeconds = 60;

int DetermineElapsedTime(const MyTime *t1, const MyTime *t2)
{//problem about static in next line
    static MyTime ((long) (((t2.hours * hourSeconds) + (t2.minutes * minSeconds) +  t2.seconds) -
                   ((t1.hours * hourSeconds) + (t1.minutes * minSeconds) + t1.seconds)));
    return(MyTime);
}

すべてではありませんが、プライマリ入力から他の入力までの時間を何らかの方法で計算する必要があります。setfill も使用する必要があります。

とにかく、静的の前に一次式が必要であるというエラーを修正する方法を知っている人はいますか?

4

3 に答える 3

2

あなたが書きたいことは(私が思うに)もっと似ています

MyTime DetermineElapsedTime(const MyTime *t1, const MyTime *t2)
{
    MyTime var = { ...... };
    return var;
}

その行の静的な背後にある意図は何でしたか? この関数を複数回呼び出すと問題が発生します。これは、その行がプログラムの存続期間中に 1 回だけ実行されるため ( を入力した場合static)、間違った答えが返されるためです。

また、戻り値の型がオフでした。そして、@mux が説明するように->/を修正する必要があります.

編集: コンストラクター構文を使用していましたが、MyTime のコンストラクターが定義されていません。MyTime var = {...}代わりに構文を使用する必要があると思いますMyTime var(...)

于 2012-11-12T05:56:24.427 に答える
1

このコードには多くの問題があります。

まず、struct型を返すことはできません。変数を返す必要があります。

静的変数を作成するときに、名前を付けて=記号を使用することに失敗しました。

->ポインタを使用する場合は、代わりに使用する必要があります.

これで、コードのロジックによって秒単位の差が得られます。次に、MyTime変数を入力するために、それを適切な時間、分、秒に変換する必要があります。時間を計算する方法の例を次に示します(テストされていません。単なる例です)。

int difference = (t2->hours * hoursSeconds + t2->minutes * minSeconds + t2->seconds) - (t1->hours * hoursSeconds + t1->minutes * minSeconds + t1->seconds); // this gives you a difference in seconds
int hoursDifference = difference / hoursSeconds; // how many full hours we have
difference -= hoursDifference * hoursSeconds; // remove from total what we just computed
int minsDifference = difference / minsSeconds; // how many full minutes we have
difference -= minsDifference * minsSeconds;
MyTime diff;
diff.hours = hoursDifference;
diff.minutes = minsDifference;
diff.seconds = difference;
return diff;
于 2012-11-12T06:04:48.657 に答える
1

MyTime変数の型と名前を忘れました。2 つのオブジェクトの違いが必要なようです。違いintは、 new の notとして返されるようMyTimeです。

int t = ((int) (((t2->hours * hourSeconds) + (t2->minutes * minSeconds) +  t2.seconds) - ((t1->hours * hourSeconds) + (t1->minutes * minSeconds) + t1->seconds)));
return(t);

また、t1t2はポインタなので、メンバーにアクセスする->代わりに使用します。変数を使用すると、変数は一度初期化され、毎回同じ値が返されます。.static

于 2012-11-12T05:57:19.347 に答える