私は学校向けのプロジェクトに取り組んでおり、このコード構造を正しく理解して動作させる必要があります。 問題:ランダムなチャンクのメモリデータを取得せずに、ユニオンメンバー(hとs)に正しくアクセスする必要があります。実際に保存した番号を返す必要があります。私は組合がどのように働くのか理解しています。なぜ私のコードが私にそこに格納された番号ではなくランダムなデータを与えているのか混乱しています。 注::私はこの問題に対する他の答えを探しましたが(これはかなり一般的な構文問題のように聞こえました)、他の解決策は私の状況にはまったく適合しませんでした。あなたがで以来、チャンクを取得しているC++ - ネストされた構造体を正しくユニオンにアクセスする
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct worker {
enum kind {NOTSALARIED, ISSALRIED};
bool isSalaried;
union {
struct hourly{
double numberHours;
double hourlyRate;
} h; // end of 'hourly' struct
struct salaried{
double salaryAmount;
double bonusAmount;
} s; // end of 'salaried' struct
} info; // end of 'info' union
};
/// PROTOTYPE section
worker* getHourly();
worker* getSalaried();
void printWorker(worker);
int main() {
worker *theWorker; // avoids making another 'worker' object for no reason
// format code for 'double' values
cout << fixed << showpoint << setprecision(2);
// used string so avoid user entering name with the correct first character
string choice;
cout << "(H)ourly or (S)alary? ";
getline(cin, choice);
/// LOGIC SECTION: Checks what the user entered
if(choice == "H" || choice == "h") {
// get the salary
theWorker = getHourly();
// pass in the dereferenced pointer to print out the info
printWorker(*theWorker);
}
if(choice == "S" || choice == "s") {
// get the salary
theWorker = getSalaried();
// pass in the dereferenced pointer to print out the info
printWorker(*theWorker);
}
return 0;
}
// method that gets the information -- creates a new worker object -- returns a pointer to the object
worker* getHourly() {
worker newWorker;
// set the isSalaried equal to false
newWorker.isSalaried = worker::NOTSALARIED; // equals 0 -- FALSE
cout << "Enter the number of hours worked: ";
// take in the hours worked
cin >> newWorker.info.h.numberHours;
cout << "Enter the hourly pay rate: ";
// take in the hourly rate
cin >> newWorker.info.h.hourlyRate;
return &newWorker;
}
worker* getSalaried() {
worker newWorker;
// set the kind equal to SALARIED
newWorker.isSalaried = worker::ISSALARIED; // equals 1 -- TRUE
cout << "Enter the salary amount: ";
// take in the salary
cin >> newWorker.info.s.salaryAmount;
cout << "Enter the bonus amount: ";
// take in the bonus
cin >> newWorker.info.s.bonusAmount;
return &newWorker;
}
void printWorker(worker theWorker) {
double grossPay;
// take care of the SALARIED first
if (theWorker.isSalaried) {
// add the salary to the bonus amount
grossPay = (theWorker.info.s.salaryAmount + theWorker.info.s.bonusAmount);
// print out the info
cout << "Salaried Worker" << endl;
// print out the salary
cout << "Salary: $" << theWorker.info.s.salaryAmount << endl;
// print out the bonus
cout << "Bonus: $" << theWorker.info.s.bonusAmount << endl;
}
// now, take care of the HOURLY
if (!(theWorker.isSalaried)) {
// get the product of the number of hours worked and the hourly rate
grossPay = (theWorker.info.h.numberHours * theWorker.info.h.hourlyRate);
// print out the info
cout << "Hourly Worker" << endl;
// print out the number of hours worked
cout << "\tHours: " << theWorker.info.h.numberHours << endl;
// print out the rate
cout << "\tRate: $" << theWorker.info.h.hourlyRate << endl;
}
// print out the last portion here, since this code will be executed for both
cout << "\t--------" << endl;
cout << "\tGross Pay: $" << grossPay << endl;
}
'リターン&newWorker;' - あなたはローカル変数 – RbMm
のアドレスを返すなぜタグCをしましたか? – kabanus
Cのプログラマーがこの状況でも助けてくれるので、私はCにタグを付けました。組合と構造体は両方ともC言語であるため。構造体は間違いありませんが、Cプログラマは依然として手助けをすることができます。 – Nik