简体   繁体   English

c ++错误c2679:二进制'[':未找到采用'SalesItem'类型的右侧操作数的运算符(或者没有可接受的转换)

[英]c++ error c2679: binary '[' : no operator found which takes a right-hand operand of type 'SalesItem' (or there is no acceptable conversion)

I am new to this site (apologies if I am doing this wrong,) and fairly new to c++. 我对这个网站是陌生的(如果我做错了,就深表歉意),对于c ++来说还是很陌生。 I have run into an annoying error with an assignment for my c++ class 我的C ++类分配时遇到了令人讨厌的错误

c++ error c2679: binary '[' : no operator found which takes a right-hand operand of type 'SalesItem' (or there is no acceptable conversion) c ++错误c2679:二进制'[':未找到采用'SalesItem'类型的右侧操作数的运算符(或者没有可接受的转换)

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(). 我已经包括了我认为最相关的类,而用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(*): 这是我发现此错误的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)
{
}

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. 我想做的是使用对象向量,而当我尝试使用该向量从SalesItems类调用display()方法时,首先发生错误。

Here is the SalesItem class: 这是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;
}

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. 这是说方括号内的内容是salesItem对象,但它期望使用整数。 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()". 不过,在这种情况下,您的for循环已经选择了该项目,因此您可以说“ item.display()”而不是“ salesItem [item] .display()”。

(You'd use salesItem[item].display() if instead your code looked sort of like this:) (如果您的代码看起来像这样,则可以使用salesItem [item] .display():)

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. 您范围圈放入的参考项目item ,而不是指数。 Instead of salesItems[item] use item . 代替salesItems[item]使用item

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C2679 二进制“-=”:未找到采用“T”类型右侧操作数的运算符(或没有可接受的转换) - C2679 binary '-=': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion) 错误1错误C2679:二进制&#39;&lt;&lt;&#39;:未找到采用“合理”类型的右侧操作数的运算符(或没有可接受的转换) - Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'rational' (or there is no acceptable conversion) 错误2错误C2679:二进制&#39;/&#39;:找不到哪个操作符采用类型的右操作数(或者没有可接受的转换) - Error 2 error C2679: binary '/' : no operator found which takes a right-hand operand of type (or there is no acceptable conversion) 错误C2679:binary&#39;=&#39;:找不到运算符,它接受类型&#39;std :: vector &lt;_Ty&gt; *&#39;的右手操作数(或者没有可接受的转换) - error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::vector<_Ty> *' (or there is no acceptable conversion) 错误 C2679:二进制“&lt;&lt;”:未找到采用“RatNum”类型的右侧操作数的运算符(或没有可接受的转换) - error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'RatNum' (or there is no acceptable conversion) 错误C2679二进制&#39;=&#39;:找不到使用&#39;int&#39;类型的右侧操作数的运算符(或者没有可接受的转换) - Error C2679 binary '=': no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) 错误:C2679二进制&#39;==&#39;:未找到采用类型为&#39;const std :: string&#39;的右侧操作数的运算符(或者没有可接受的转换 - Error:C2679 binary '==': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion 错误C2679:二进制&#39;&gt;&gt;&#39;:未找到采用&#39;const char [4]&#39;类型的右侧操作数的运算符(或没有可接受的转换)22 - Error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [4]' (or there is no acceptable conversion) 22 错误C2679:二进制&#39;&gt;&gt;&#39;:找不到哪个运算符采用&#39;std :: string&#39;类型的右手操作数(或者没有可接受的转换) - error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) 错误 C2679:二进制“&lt;&lt;”:未找到采用“mystring”类型的右侧操作数的运算符(或没有可接受的转换) - error C2679: binary '<<': no operator found which takes a right-hand operand of type 'mystring' (or there is no acceptable conversion)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM