2016-05-14 10 views
-3

私はC++コードを書いていますが、上記のタイトルにはいくつかの行に誤りがあります。なぜこれが起こっているのか分かりません。エラー:演算子[]の一致がありません

CODE:

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

だから、私の考えは正しかった:

#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 

     ... 

    } 

一つの考えが私の心には、次の形式の関数を作成することでした交差?コードで何を変更すればよいですか?

私が言及したエラーを修正するにはどうすればよいですか?

ありがとうございます!

+0

なぜこのコードがインライン化されることを望んでいますか? –

+0

..それはたくさんの変数の一つです。 –

+0

は 'categories'ではありません'リスト 'ですか?私が思い出す限り、これは '[]'演算子を提供しません。 – ale64bit

答えて

1

categoriesはリストであり、ベクターではありません。したがって、エラーメッセージが表示されます。

関連する問題