2015-05-09 41 views
-2
#include <iostream> 
#include <queue> 

using namespace std; 

int main() { 
    struct process { 
     int burst; 
     int ar; 
    }; 
    int x=4; 
    process a[x]; 
    queue <string> names; /* Declare a queue */ 
    names.push(a[1]); 
    return 0; 
} 

我想在隊列中推動結構變量,但它不是服用,並給出錯誤推動結構變量在隊列

no matching function for #include queue and invalid argument 

我該怎麼辦呢?

+0

這當然是一個完全錯誤的方法:'process a [x];' –

+0

@πάνταῥεῖ所以你說我甚至不能創建一個結構數組? 因爲它沒有給出錯誤{process a [x]}我測試過了,並且還分配了突發和到達整個數組並打印出它的工作正常。 –

+0

你可以但這不是你如何創建一個數組。尺寸甚至會是多少? – Scott

回答

0

C++是一種強類型語言。在names.push(a[1]);行中,您嘗試將struct(從您的process a[x];陣列)推送到queue<string>。你的結構不是string,所以編譯器會發出錯誤。你至少需要一個queue<process>

其他問題:可變長度數組不是標準的C++(process a[x];)。改爲使用std::vector<process>。下面是工作一些簡單的例子:

#include <iostream> 
#include <queue> 
#include <string> 
#include <vector> 

using namespace std; 

int main() { 
    struct process // move this outside of main() if you don't compile with C++11 support 
    { 
     int burst; 
     int ar; 
    }; 
    vector<process> a; 
    // insert two processes 
    a.push_back({21, 42}); 
    a.push_back({10, 20}); 

    queue <process> names; /* Declare a queue */ 
    names.push(a[1]); // now we can push the second element, same type 
    return 0; // no need for this, really 
} 

EDIT

本地定義的類/用於實例化的模板結構僅在C++ 11和後是有效的,例如參見Why can I define structures and classes within a function in C++?以及其中的答案。如果您無法使用符合C++ 11的編譯器,請將您的struct定義移至main()之外。

+0

它給4個錯誤的(矢量一個;) 1.trying實例「模板類的std ::分配器 2.參數無效 –

+0

編譯與'-std = C++ 11' – vsoftco

+0

@RanaJunaidJaved看到更新編輯。 – vsoftco