错误:无法匹配运算符[]

时间:2016-05-14 20:24:11

标签: c++

我正在编写一个C ++代码,我在上面提到了标题提到的错误。我不知道为什么会这样。

CODE:

#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; // A vector describing the supermarket cashiers declaring a queue of customers for each cashier
    cashiers.reserve(num_cashiers); // I create as many cashiers as the user wants.
    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;
    int number_new_customers;
    int number_products;
    string new_answer;
    int 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) //
        {
         ...



        // Creation of the list with the totally paid amount for each category by the customers
        //for (int &i : categories_prices) categories_prices[i] = 0;
        for (int i = 0; i < customers.size(); i++)
        {
            for (int j = 0; j < products.size(); j++)
            {
                Products products[i][j];
                if (products[i][j].category == categories[i]) // HERE I AM GETTING THE ERROR
                    categories_prices = categories_prices + products[i][j].price; // HERE I AM GETTING AN NO MATCH FOR OPERATOR + ERROR
            }
        }

        // Statistical mapping
        for (int i = 0; i < categories.size(); i++) statistics[categories[i]] = categories_prices[i]; // HERE I AM GETTING THE ERROR

         ...

  }

我想到的一个想法是创建以下形式的函数:

int* operator[](int index)
{
   return arr[index]; // where arr could be the name of any vector of mine
}

那么,我的想法是否正确?我应该在代码中更改什么?

如何修复我提到的错误?

提前谢谢!

1 个答案:

答案 0 :(得分:1)

categories是一个列表,而不是矢量。因此,您收到错误消息。