-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;
}

I'm trying to pushing struct variable in queue but its not taking it and gives errors

no matching function for #include queue and invalid argument

how can I do that?

6
  • This is certainly a completely wrong approach: process a[x]; Commented May 9, 2015 at 18:09
  • @πάνταῥεῖ so u are saying i cant even create an array of structure? Because it does not give error {process a[x]} i have tested and also assigned burst and arrival to whole array and printed its working fine. Commented May 9, 2015 at 18:10
  • You can but that isn't how you create an array. What would the size even be? Commented May 9, 2015 at 18:13
  • You can do that, but your approach (to solve whatever) is pretty nonsensical and wrong (there's no, or just undefined memory allocated for a, snce x is uninitialized). Commented May 9, 2015 at 18:13
  • @Scott size is x which is i forget to initialize but in the other code where m working there is no error about process's array. Commented May 9, 2015 at 18:14

1 Answer 1

0

C++ is a strongly typed language. In the line names.push(a[1]); you are trying to push a struct (from your process a[x]; array) into a queue<string>. Your struct is not a string, so the compiler will emit an error. You at least need a queue<process>.

Other issues: variable length arrays are not standard C++ (process a[x];). Use a std::vector<process> instead. Here is some simple example that works:

#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

Locally defined classes/structs used to instantiate templates are valid only in C++11 and later, see e.g. Why can I define structures and classes within a function in C++? and the answers within. If you don't have access to a C++11 compliant compiler, then move your struct definition outside of main().

Sign up to request clarification or add additional context in comments.

4 Comments

it is giving 4 errors at (vector<process> a;) 1.trying to instantiate 'template<class> class std::allocator 2. Invalid argument
Compile with -std=c++11
thanks it really worked. I'm wondering if u tell me please why did u use a.push_back({21,42}) in the code. what is this ?
{21, 42} creates a temporary object of type struct process, which is then inserted in the vector via the push_back vector's member function. See e.g. this for a tutorial about std::vector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.