I am new to this site (apologies if I am doing this wrong,) and fairly new to c++. I have run into an annoying error with an assignment for my c++ class
c++ error c2679: binary '[' : no operator found which takes a right-hand operand of type 'SalesItem' (or there is no acceptable conversion)
I'm not really sure what I need to include to help get an answer. I have included what I think to be the most relevant classes, leaving out the class with main(). I have not included the headers, but I should be able to add those in if needed...
Here is the Invoice class where I am finding this error, I have marked the lines where the error occurs with astriks(*):
#include "stdafx.h"
#include <iostream>
#include "Invoice.h"
#include <vector>
#include <string>
using namespace std;
static int nextInvoiceNum = 1000;
Invoice::Invoice(const string name, const int vectorSize)
{
customerName = name;
salesItems.reserve(vectorSize);
invoiceNumber = nextInvoiceNum++;
}
void Invoice::setCustomerName(string name)
{
customerName = name;
}
string Invoice::getCustomerName()
{
return customerName;
}
int Invoice::getInvoiceNumber()
{
return invoiceNumber;
}
void Invoice::display()
{
cout << "-------------------------\n";
cout << "\n Invoice #: " << getInvoiceNumber() << "\n";
cout << "\n Customer name: " << getCustomerName() << "\n";
cout << "-------------------------\n";
cout << "Items Purchased";
cout << "-------------------------\n";
for (auto &item : salesItems)
{
********cout << salesItems[item].display() << "\n";
cout << "-------------------------\n";
}
}
void Invoice::addSalesItem(SalesItem&)
{
salesItems.push_back(SalesItem());
}
double Invoice::getInvoiceAmount()
{
double runningTot = 0;
double total = 0;
for (auto &item : salesItems)
{
********runningTot = salesItems[item].getPartQuantity() * salesItems[item].getPartPrice();
total += runningTot;
}
return total;
}
Invoice::~Invoice(void)
{
}
What I am attempting to do is use a vector of objects and where the error first occurs is when I try to call the display() method from the SalesItems class using the vector.
Here is the SalesItem class:
#include "stdafx.h"
#include "SalesItem.h"
#include <iostream>
using namespace std;
SalesItem::SalesItem(string partNumber, string partDescription, int partQuantity, double partPrice)
{
partNumber = "";
partDescription = "";
partQuantity = 0;
partPrice = 0;
}
SalesItem::~SalesItem(void)
{
}
void SalesItem::setPartNumber(string number)
{
partNumber = number;
}
void SalesItem::setPartDescription(string description)
{
partDescription = description;
}
void SalesItem::setPartQuantity(int quantity)
{
partQuantity = quantity;
}
void SalesItem::setPartPrice(double price)
{
partPrice = price;
}
string SalesItem::getPartNumber()
{
return partNumber;
}
string SalesItem::getPartDescription()
{
return partDescription;
}
int SalesItem::getPartQuantity()
{
return partQuantity;
}
double SalesItem::getPartPrice()
{
return partPrice;
}
void SalesItem::display()
{
cout<< "-------------------------:";
cout<< "\n Part number: " << getPartNumber() << "\n";
cout<< "\n Part description: " << getPartDescription() << "\n";
cout<< "\n Part quantity: " << getPartQuantity() << "\n";
cout<< "\n Part price: $" << getPartPrice() << "\n";
cout<< "\n Invoice amount: $" << getInvoiceAmount() << "\n";
}
double SalesItem::operator+(double total)
{
double subtotal = getPartQuantity() * getPartPrice();
total += subtotal;
return total;
}
double SalesItem::getInvoiceAmount()
{
double invoice = getPartQuantity() * getPartPrice();
return invoice;
}
Again, sorry if I'm posting this in the wrong way. I would really appreciate some help with this. Will provide any other necessary info or make changes on a timely basis.
Thanks!
It's saying that the thing inside the square brackets is a salesItem object, but it was expecting an integer. The normal use of square brackets is to pick out one item out of a collection, using some kind of index, usually an integer. In this case, though, your for loop has already picked the item out, so you can just say "item.display()" instead of "salesItem[item].display()".
(You'd use salesItem[item].display() if instead your code looked sort of like this:)
int item;
for (item=0; item < ?however many sales items you have? ; item++) {
bla bla salesItem[item].display()
}
Your range for loop puts the reference to the item in item
, not the index. Instead of salesItems[item]
use item
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.