2016-05-12 4 views
-2

私はC++でいくつかのコードを作成しようとしています。私は、顧客と製品が事前に宣言されているベクトルであることを明確にする必要がありエラー:型 'something'の式から 'int&'型の参照の初期化が無効です

#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」、2番目には「Products *」 となります。

for-loopメソッドで何か問題があるのを恐れています。私は理解できる限りfor_eachループまたはfor-loopを自分のやり方でやろうとしました。ベクトル顧客のサイズ(それぞれ、製品ベクトルのサイズを通じたjの大きさ)を介してiを参照すると、何かが間違っている可能性があります。

私のループには何が問題なのですか?どのように修正できますか?

+0

明らかに、これらのループは、あなたが期待していることをしていませんに。 ranged-forループを使用する方法を調べてください。 – chris

+1

[mcve]を入力してください。 – Barry

+0

あなたのタイプが混ざっているようです。 「顧客」とは何ですか? 'int'を保持するコンテナのようにループします。しかし、あなたは 'customers [i] .reserve()'と呼びますが、 'customers'は実際に' vector'sか何かを保持するコンテナです。 – TFM

答えて

0

私は、製品のベクトルを保持する顧客クラスを持つことをお勧めします。また、あなたの顧客を保持するキャッシャークラス。あなたが顧客のベクトルを持っているとします。

std::vector<Customer> customers; 

あなたはベクトル

のベクトルで、それを使用したい場合は、反復

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

OR

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

要素にアクセスするループに使用する2つの方法があります。

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

私の投稿を編集しました。必要に応じて、回答を編集するためにもう一度見てください。 –

+0

私はエラーを解決しました。私はちょうど内部ループのために(auto i:customers)のために書いています。 –

+0

は自動的に正しいデータを保存するため、自動動作します。オートストアコピーとオート&参照を格納する – Striker

関連する問題