我对这个网站是陌生的(如果我做错了,就深表歉意),对于c ++来说还是很陌生。我的C ++类分配时遇到了令人讨厌的错误
c ++错误c2679:二进制'[':未找到采用'SalesItem'类型的右侧操作数的运算符(或没有可接受的转换)
我不确定我需要包括什么来帮助获得答案。我已经包括了我认为最相关的类,而用main()忽略了该类。我没有包括标题,但是如果需要,我应该能够在其中添加标题...
这是我发现此错误的Invoice类,我用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)
{
}
我试图做的是使用对象的向量,而当我尝试使用该向量从SalesItems类中调用display()方法时,首先出现该错误。
这是SalesItem类:
#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;
}
再次,抱歉,如果我以错误的方式发布此信息。我真的很感谢您的帮助。将提供任何其他必要的信息或及时进行更改。
谢谢!
最佳答案
这是说方括号内的内容是salesItem对象,但它期望使用整数。方括号的通常用法是使用某种索引(通常是整数)从集合中选择一项。不过,在这种情况下,您的for循环已经选择了该项目,因此您只能说“ item.display()”而不是“ salesItem [item] .display()”。
(如果您的代码看起来像这样,则可以使用salesItem [item] .display():)
int item;
for (item=0; item < ?however many sales items you have? ; item++) {
bla bla salesItem[item].display()
}