2016-05-14 73 views
-3

我正在寫一個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 
} 

所以,當時我的想法是否正確?我應該在代碼中更改哪些內容?

如何解決我提到的錯誤?

預先感謝您!

+0

你爲什麼希望這段代碼被內聯? –

+0

..這就是很多變數的一個地獄 –

+0

不是'categories' a'list '?據我記得,它不提供'[]'運算符。 – ale64bit

回答

1

categories是一個列表,而不是一個向量。因此你會收到錯誤信息。