Question: I'm having trouble fixing the errors in the classes in my program vending.cpp:134:24: error: expected unqualified-id VendingProduct = new Product[ProductLimit]; ^ vending.cpp:157:35: warning: expression result
I'm having trouble fixing the errors in the classes in my program
vending.cpp:134:24: error: expected unqualified-id
VendingProduct = new Product[ProductLimit];
^
vending.cpp:157:35: warning: expression result unused [-Wunused-value]
VendingProduct Product = (Name, Count, Cost);
^~~~
vending.cpp:157:41: warning: expression result unused [-Wunused-value]
VendingProduct Product = (Name, Count, Cost);
^~~~~
vending.cpp:157:24: error: no viable conversion from 'double' to
'VendingProduct'
VendingProduct Product = (Name, Count, Cost);
^ ~~~~~~~~~~~~~~~~~~~
vending.cpp:21:7: note: candidate constructor (the implicit copy constructor)
not viable: no known conversion from 'double' to 'const VendingProduct &'
for 1st argument
class VendingProduct
^
My Code:
class VendingProduct
{
public:
char* Name;
int Count;
double Cost;
VendingProduct(char *name, int count, double cost)
{
Name = name;
Count = count;
Cost = cost;
}
string getName()
{
if(Count >= 1){
return Name;
}
else {
return "Empty";
}
}
int getCount()
{
return Count;
}
double getCost()
{
if (Count >= 1){
return Cost;
}
else{
return 0.0;
}
}
void purchase(int newPurchase)
{
if (Count >= 1){
Count = Count - newPurchase;
newPurchase *= Cost;
}
else {
return ;
}
}
void restock(int newStock)
{
Count = Count + newStock;
}
double setCost(double newCost)
{
return Cost = newCost;
}
};
class VendingMachine
{
public:
double Bank;
VendingProduct *Products;
int ProductNum;
char *Name;
int Count;
double Cost;
VendingMachine(int ProductLimit)
{
this->Bank = 0;
VendingProduct = new Product[ProductLimit];
}
void restock(char *name, int quantity, double cost)
{
for(int i = 0; i < this->ProductNum; i++){
if(this->Products[i].Name==name){
this->Products[i].Count = quantity;
this->Products[i].Cost = cost;
break;
}
}
VendingProduct Product = (Name, Count, Cost);
for(int i = 0; i < this->ProductNum; i++){
if(this->Products[i].Count==0){
this->Products[i].Name = name;
this->Products[i].Count = quantity;
this->Products[i].Cost = cost;
break;
}
}
this->Products[this->ProductNum] = Product;
this->ProductNum += 1;
}
void purchase(int items)
{
if(items >= this->ProductNum){
return;
}
if(this->Products[items].Count < 1){
return;
}
this->Products[items].Count -= 1;
this->Bank += this->Products[items].Cost;
}
double getBank()
{
return this->Bank;
}
string getName(int items)
{
if(items >= this->ProductNum){
return "Invalid selection";
}
if(this->Products[items].Count < 1){
return "Empty";
}
return this->Products[items].Name;
}
int getCount(int items)
{
if(items >= this->ProductNum){
return 0;
}
if(this->Products[items].Count < 1){
return 0;
}
return this->Products[items].Count;
}
double getCost(int items)
{
if(items >= this->ProductNum){
return 0.0;
}
if(this->Products[items].Count < 1){
return 0.0;
}
return this->Products[items].Cost;
}
};
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
