错误:'int&'类型引用的初始化无效来自“某事物”类型的表达

时间:2016-05-12 22:45:55

标签: c++ c++11 vector

我正在尝试使用c ++ 11的一些功能在c ++中创建一些代码。我正在使用矢量,列表等......

我的代码中有争议的部分如下所示:

#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

我必须明确客户和产品是事先声明过的载体。 此外,错误发生的第一行的名称是'std :: vector','Products *' 第二个。

我担心我的for-loop方法出错了。据我所知,我试图以我的方式做一个for_each循环或一个for-loop范围。通过矢量客户的大小来引用i可能有问题(分别通过产品向量的大小)。

我的循环有什么问题,我该如何解决?

1 个答案:

答案 0 :(得分:0)

我建议有一个Customer类,其中包含产品的向量。还有一个收银员类来容纳您的客户。假设你有一个顾客载体。

std::vector<Customer> customers;

有两种方法可以使用for循环迭代访问元素

for (int i = 0; i < customers.size; i++)
{
    customers[i]; //access element
}

OR

for (Customer customer: customers)
{
    customer;//access element
}

如果你想在矢量载体中使用它

for (int i = 0; i < customers.size; i++)
{
    for (int j = 0; j < customers[i].size; j++)
    {
        customers[i][j]; //access element
     }
}