我有一个类对象Stock,在Stock 3中存在InventoryName、条形码和InventoryQuantity的实体。用户输入填充这些实体,并将对象传递给向量以进行存储。我的问题在于,当我尝试搜索该实体的向量以便删除它时,请参阅源代码和removeItem函数,我尝试了向量上的binary_search并得到了一个错误,当我使用一个find()函数时,它会给出一个错误“二进制”=‘=’没有找到的操作符,它使用'const‘类型的左手操作数。到目前为止我的代码..。我的类头文件
//inventory.h
#include <iomanip>
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
class Stock {
public:
// Initialise object of class Stock with data members
explicit Stock(std::string, std::string, std::string);
// Prototype for member function DisplayStckItem, Displays entire stock
void displayStckItem(std::string, std::string, std::string) const;
//function prototype to add items to the vector array invName (Setters)
void setItemName(std::string);
void setBarcode(std::string);
void setInvQty(std::string);
//getters for the data members housed in the vector
std::string getInventoryName() const;
std::string getBarcode() const;
std::string getInvQty() const;
// function to remove stock item based on matched arguments
void removeItem(const std::vector<Stock>&);
// function to display all stock
void displayTotal(const std::vector <Stock> &);
private:
std::string InventoryName; //data member of stock name
std::string Barcode; // data member of stock barcodes
protected:
std::string InventoryQty; //data member for stock levels
};来源
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <cmath>
#include <string>
#include <vector>
#include <regex>
#include "inventory.h"
using namespace std;
Stock::Stock(string code, string iName, string iQuantity)
: Barcode(code), InventoryName(iName), InventoryQty(iQuantity) //member initiailiser list
{
};
void Stock::displayStckItem(string x, string y, string z) const {
}
//setters
void Stock::setItemName(string newItemName) {
bool valiName;
do {
cout << "Please enter item name: " << endl;
cin.ignore();
getline(cin, newItemName);
if (regex_match(newItemName, regex("^[A-Z a-z]+$")) == true && newItemName.size() <= 15)
valiName = true;
else {
cout << "Invalid input, No numbers allowed in inventory name and only 15 cahracters long" << endl;
valiName = false;
}
} while (valiName == false);
InventoryName = newItemName;
}
void Stock::setBarcode(string newItemCode) {
bool valiCode;
do {
cout << "Please enter barcode: " << endl;
cin >> newItemCode;
if (regex_match(newItemCode, regex("^[0-9]+$")) == true && newItemCode.size() == 6) {
valiCode = true;
}
else {
cout << "Invlaid input, values must be number values and 6 characters long" << endl;
valiCode = false;
}
} while (valiCode == false);
Barcode = newItemCode;
}
void Stock::setInvQty(string newItemQty) {
bool valiQty;
do {
cout << "Please enter item quantity: " << endl;
cin >> newItemQty;
if (regex_match(newItemQty, regex("^[0-9]+$")) == true && newItemQty.size() <= 6) {
valiQty = true;
}
else {
cout << "Invalid input, input number value with max of 999,999" << endl;
}
} while (valiQty == false);
InventoryQty = newItemQty;
}
void Stock::removeItem(const vector <Stock> &a) {
string invItem;
cin.ignore();
getline(cin, invItem);
bool found = binary_search(a.begin(), a.end(), InventoryName);
if (invItem == InventoryName) {
cout << "Item found" << endl;
}
else {
cout << "Item not found" << endl;
}
}
void Stock::displayTotal(const vector <Stock> &a) {
cout << "Items held in inventory" << endl;
for (Stock const &item : a) {
cout << item.getInventoryName() << setw(16) << item.getBarcode() << setw(8) << item.getInvQty() << endl;
}
}
//getters
string Stock::getInventoryName() const{
return InventoryName;
}
string Stock::getBarcode() const {
return Barcode;
}
string Stock::getInvQty() const {
return InventoryQty;
}司机
#include "inventory.h"
#include <vector>
using namespace std;
int main() {
vector<Stock> range;
string temp1;
string temp2;
string temp3;
char options;
bool prog; //boolean to repeat and exit main program
//Instantiate stock object for passing to vector
Stock Stock1(temp1, temp2, temp3);
cout << "Welcome to your inventory management system!" << endl;
do {
cout << "\nPlease enter a number from the following options" << endl;
cout << "To add a product: 1\nTo remove a product: 2\nTo veiw product inventory: 3\nTo exit the program: 4" << endl;
cin >> options;
if (options >= 49 && options <= 52) { // test char options within the ascii range between 1 and 4
switch (options)
{
case '1':
Stock1.setItemName(temp2);
Stock1.setBarcode(temp1);
Stock1.setInvQty(temp3);
range.push_back(Stock1);
cout << "New product added" << endl;
prog = true;
break;
case '2': cout << "Please input a product name or barcode" << endl;
Stock1.removeItem(range);
prog = true;
break;
case '3': cout << "Veiw product" << endl;
Stock1.displayTotal(range);
prog = true;
break;
case '4': cout << "Exit program" << endl;
prog = false;
break;
}
}
else {
cout << "Your input is not within the options list" << endl;
}
} while (prog == true);
}我提供了一切,因为我正在学习c++和对风格的反馈,否则我可能犯的任何其他错误都会很棒。希望我的帖子也能帮助其他人。
发布于 2018-12-09 17:36:07
所以我找到了一个可能不是很好的解决方案,我想搜索对象实体的向量并返回一个索引。
void Stock::removeItem(vector <Stock> &a) {
string invItem, name;
int i = 0;
cin.ignore();
getline(cin, invItem);
for (Stock const &item : a) {
name = item.getInventoryName();
if (name == invItem) {
cout << "item found: " << i << endl;
a.erase(a.begin() + i);
break;
}
else if (a.empty()) {
cout << "No items currently in inventory" << endl;
}
else {
cout << "Item not in inventory" << endl;
++i;
}
}
}如果这是浪费或脆弱,我会欢迎反馈。
发布于 2018-12-08 17:05:11
您应该重载“Stock”类的相等运算符:
class Stock {
(...)
bool operator==(const Stock& lhs, const Stock& rhs) {
return lhs.InventoryName == rhs.InventoryName && lhs.Barcode == rhs.Barcode && lhs.InventoryQty == rhs.InventoryQty;
}
(...)
}https://stackoverflow.com/questions/53684635
复制相似问题