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)
我是这个网站的新手(如果我做错了,很抱歉),对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()
}
循环的范围将引用放在item
中,而不是索引中。使用item
代替salesItems[item]
。
相关文章:
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 是否所有C++运算符都接受操作数?
- 一元*运算符的操作数是否期望一个 prvalue
- C2678 二进制 '==':未找到采用 'Card' 类型左操作数的运算符(或者没有可接受的转换)
- 我的运算符重载是否有效<<(流插入)左操作数不是 ostream
- 没有运算符"="与这些操作数匹配,JUCE 窗口演示应用程序
- C2679 二进制"<<":未找到采用右操作数类型的运算符
- CPP 运算符重载操作数位置 [] 与 ==
- int* 和 int[] 类型对二进制运算符 + 的操作数无效
- 运算符'<'不能应用于类型 'string' 和 'string' 的操作数
- 错误 C2679 二进制"<<":未找到采用类型为 'T' 的右侧操作数的运算符
- 是否允许三元运算符在C++中计算两个操作数?
- 二进制 '==':未找到采用 'Enemy' 类型左侧操作数的运算符(或者没有可接受的转换)
- 与"运算符>>"不匹配(操作数类型为"QDataStream"和"QJsonObject")
- 类型"int"和"const char [15]"到二进制"运算符<<"的无效操作数^
- 如何摆脱逗号运算符的这个错误右操作数没有效果( wunsued 值)?
- c++ 错误:二进制"运算符+"类型"矩阵*"和"矩阵*"的操作数无效
- c++运算符重载-我实际返回的操作数类型是什么
- 没有运算符>>与这些操作数匹配 ifstream