gpt4 book ai didi

C++ 错误 c2679 : binary '[' : no operator found which takes a right-hand operand of type 'SalesItem' (or there is no acceptable conversion)

转载 作者:行者123 更新时间:2023-11-28 06:55:58 26 4
gpt4 key购买 nike

我是这个站点的新手(如果我做错了,我深表歉意),而且是 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)
{
}

我试图做的是使用一个对象 vector ,当我尝试使用该 vector 从 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()
}

关于C++ 错误 c2679 : binary '[' : no operator found which takes a right-hand operand of type 'SalesItem' (or there is no acceptable conversion),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23174429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com