所以,我正在編寫一個代碼來計算基於銀行交易的金額。 我創建了一個枚舉類來標識SALE = 1,VALUEADDED = 2,CREDIT = TransactionType是枚舉。C++的枚舉顯示錯誤:「非靜態成員引用必須相對於特定對象。」
class transaction {
public:
enum class TransactionType { sale = 1, valueadded, credit, cancel, promo, discount, interterritory };
transaction(int transactionId, int salerepId, int transactionType, int amount) {
this->transactionId = transactionId;
this->salerepId = salerepId;
this->transactionType = static_cast<TransactionType>(transactionType);
this->amount = amount;
}
void print() {
cout << transactionId << "\t\t" << salerepId << "\t\t";
switch (transactionType)
{
case TransactionType::sale:
cout << "SALE";
break;
case TransactionType::valueadded:
cout << "VALUEADDED";
break;
case TransactionType::credit:
cout << "CREDIT";
break;
case TransactionType::cancel:
cout << "CANCEL";
break;
case TransactionType::promo:
cout << "PROMO";
break;
case TransactionType::discount:
cout << "DISCOUNT";
break;
case TransactionType::interterritory:
cout << "INTERTERRIOTY";
break;
default:
cout << "UNIDENTIFIED";
break;
}
cout << "\t\t" << amount << endl;
}
TransactionType transactionType;
private:
int transactionId;
int salerepId;
int amount;
};
下面的代碼是我想根據什麼類型的交易來計算金額。 (Territory_output是給出金額輸出的另一個類,salerep :: amount是來自不同類的另一個實例,它提供了初始金額)。
territory_output tAmount;
if (transaction::transactionType == transaction::TransactionType::sale) {
tAmount.territoryAmount += salerep::amount * 1.1;
return tAmount.territoryAmount;
}
if (transaction::transactionType == transaction::TransactionType::valueadded) {
tAmount.territoryAmount += salerep::amount * 1.1;
return tAmount.territoryAmount;
}
if (transaction::transactionType == transaction::TransactionType::credit) {
tAmount.territoryAmount -= salerep::amount * 1;
return tAmount.territoryAmount;
}
if (transaction::transactionType == transaction::TransactionType::cancel) {
tAmount.territoryAmount -= salerep::amount * 1.25;
return tAmount.territoryAmount;
}
if (transaction::transactionType == transaction::TransactionType::promo) {
tAmount.territoryAmount -= salerep::amount * 0;
return tAmount.territoryAmount;
}
if (transaction::transactionType == transaction::TransactionType::discount) {
tAmount.territoryAmount -= salerep::amount *1.1;
return tAmount.territoryAmount;
}
if (transaction::transactionType == transaction::TransactionType::interterritory) {
tAmount.territoryAmount += salerep::amount *0.75;
return tAmount.territoryAmount;
}
我得到一個錯誤「非靜態成員引用必須是相對於特定對象。」用於將transaction :: TransactionType作爲枚舉進行調用。 和我得到相同的錯誤「非靜態成員引用必須相對於特定的對象。」 for salerep ::金額。 有人可以幫忙嗎? 這是表格的圖片,用於說明歸屬。 The table of Transaction Type & Attribution Rule
它不是'enum' - 你'transaction'類沒有名爲'transactionType' – UnholySheep
你想指與'交易:: transactionType'什麼'static'會員? – user463035818