C++ error: ‘stack’ is not a member of ‘std’


#1
#include <iostream>
using namespace std;

int main()
{
    std::stack<int> p;
    cout << "hello world!" << endl;
}

when I use g++ to compile the code above. I receive that:

zhengyangtongxie:~/workspace $ g++ test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:6:5: error: ‘stack’ is not a member of ‘std’
     std::stack <int> p;
     ^
test.cpp:6:17: error: expected primary-expression before ‘int’
     std::stack <int> p;
                 ^
test.cpp:6:17: error: expected ‘;’ before ‘int’

I don’t understand why the error received. Thank you very much.


#2

Hi,

Thank you for writing in. In order to use stack within your code, you need to include <stack>. iostream doesn’t define it which is why you’re seeing the error.

For more reference: http://www.cplusplus.com/reference/stack/stack/stack/

For purely code related issues, please post on StackOverflow.com. If you have any Cloud9 specific issues or questions though, we’ll be happy to help.

Have a great day!

Regards,
​Mutahhir


#3

I have solved this problem.

add:

#include <stack> 

at the begin of the code.

Thank you. @mutahhir