Ich versuche, etwas Code in C++ mit einigen Funktionen von C++ 11 zu machen. Ich bin mit Vektoren, Listen und so weiter ...Fehler: ungültige Initialisierung der Referenz des Typs 'int &' vom Ausdruck des Typs 'etwas'
Der strittige Teil meines Codes unten dargestellt wird:
#include <iostream>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <map>
#include <algorithm>
#include <list>
//#include <Winbase.h>
using namespace std;
// A struct describing a product.
typedef struct Products
{
string category;
string name;
float price;
} Product;
inline void scenario1(int num_cashiers)
{
vector<Product> products; // It is a vector(a pseudo-second dimension) of products which will be used for each customer
vector<vector<Product>> customers; // A vector containing all customers
vector<vector<vector<Product>>> cashiers(num_cashiers); // A vector describing the supermarket cashiers declaring a queue of customers for each cashier
double start = GetTickCount(); // It will be used for counting 10 secs until next update
vector<int> total_products(num_cashiers); // A vector keeping the total number of products of each queue
list<string> categories; // A list containing all the categories of the products
list<float> categories_prices; // A list containing all category prices
map<string,float> statistics; // A map that keeps the statistical report of the supermarket. It keeps the name of each category and the total amount having been paid by customers for products of this category
string want_new_customers;
short number_new_customers;
short number_products;
string new_answer;
short pos_min_cashier;
string seeQueue;
int select_cashier;
string seeAvgTime;
string seeStatistics;
while (true)
{
double current_time = GetTickCount() - start; // We are taking each and every second.
// Update every 10 secs (=10000msecs)
if (current_time >= 10000) //
{
cout << "Would you like to add some customers?(Y or N)" << endl;
cin >> want_new_customers;
if (want_new_customers == "Y" || want_new_customers == "y")
{
cout << "How many customers would you like to add?" << endl;
cin >> number_new_customers;
customers.reserve(number_new_customers);
for (int &i : customers) //HERE IS THE FIRST LINE I AM GETTING THE ERROR
{
cout << "Give some necessary information about customer no. " << i << endl;
cout << "Give the number of products he/she bought" << endl;
cin >> number_products;
customers[i].reserve(number_products);
Products products[number_new_customers][number_products];
for (int &j : products) //HERE IS THE SECOND ONE
{
cout << "Give the category of the product no. " << j << endl;
cin >> products[i][j].category;
cout << "Give the name of the product no. " << j << endl;
cin >> products[i][j].name;
cout << "Give the price of the product no. " << j << endl;
cin >> products[i][j].price;
}
}
}//AND THE CODE GOES ON
ich machen müssen klar, dass Kunden und Produkte Vektoren sind, die vorher erklärt worden sind . Auch der Name ist "Std :: Vektor" für die erste Zeile der Fehler auftritt und "Produkte *" für die zweite.
Ich habe Angst, dass etwas mit meiner For-Loop-Methode falsch ist. Ich habe versucht, eine for_each-Schleife oder eine for-Schleife auf meine Art zu machen, soweit ich das verstehe. Etwas kann falsch sein bei der Referenzierung von i durch die Größe der Vektorkunden (von j durch die Größe des Produktvektors).
Was ist los mit meinen Schleifen und wie kann ich es beheben?
Offensichtlich tun diese Schleifen nicht das, was Sie von ihnen erwarten zu. Sehen Sie nach, wie Sie eine Entfernungsschleife verwenden. – chris
Bitte geben Sie eine [mcve] an. – Barry
Sie scheinen Ihre Typen durcheinander zu bringen. Was sind Kunden? Du schlingst darüber hinweg, als wäre es ein Container, der 'int' enthält. Aber dann nennst du 'customers [i] .reserve()', jetzt sieht es aus wie 'customers' ist eigentlich ein container, der' vector's oder sowas hält. – TFM