0
我試過尋找解決方案,但在這些問題的答案是非常具體的。這是我的。將對象分配給另一個對象時,沒有可行的重載'='?
標題代碼。
class InventoryItem {
public:
InventoryItem(char *desc = 0, double c = 0, int u = 0);
~InventoryItem();
char *getDescription() const;
double getCost() const;
int getUnits() const;
void operator= (const InventoryItem &right);
int setUnits;
private:
char *description;
double cost;
int units;
};
與頭文件關聯的.cpp文件。
InventoryItem::InventoryItem(char *desc, double c, int u) {
description = desc;
cost = c;
units = u;
}
InventoryItem::~InventoryItem() {}
void InventoryItem::operator= (const InventoryItem &right) {
units = right.getUnits();
cost = right.getCost();
description = right.getDescription();
}
CashRegister類。
class CashRegister {
public:
void getItemToPurchase(InventoryItem) const;
private:
InventoryItem item;
}
在此代碼中發生錯誤,該代碼是CashRegister函數的.cpp文件。
void CashRegister::getItemToPurchase(InventoryItem item1) const {
item = item1;
}
它說:「沒有可行的操作符」 =」,即使項目和物品1是同一類的兩個對象。
我比我的代碼到一個在書,但它仍然沒有按」噸工作。
http://stackoverflow.com/help/mcve –
最起碼,包括你寫的_full_錯誤信息。更理想的是MCVE會有所幫助。 – TheUndeadFish
完整的錯誤是「No viable overloaded'='這是所有的程序顯示 –