C++: 'set' and 'vector' "undeclared despite #include statements

删除回忆录丶 提交于 2019-12-24 16:18:35

问题


I am using Netbeans 7.1 on Ubuntu 11.04.

The following call

set< Triangle > V;

gives the error message

error: ‘set’ was not declared in this scope

and the following call

vector< Triangle > ans;

gives the error message

error: ‘vector’ was not declared in this scope

This despite my having

#include <vector>
#include <set>
#include <map>

at the beginning of the C++ file.

At help resolving this would be greatly appreciated.
Peter.


回答1:


you forgot about namespace std :

std::set< Triangle > V; std::vector< Triangle > V;




回答2:


Vectors Sets and map are part of the c++ Standard Library so you need to call vector/set/map with

std::vector< Triangle > ans;

or add

using namespace std;

after the include statements.




回答3:


They live in the std namespace. So, either fully quality the types (std::vector) or use a using statement (using namespace std;).

The latter option pollutes the global namespace. Never do that in a header file (otherwise the entire namespace is imported when you include the header) and only do it in your implementation file if you know that it isn't going to cause any collisions.

#include <vector>

int main(...) {
    vector v;      // no worky
    std::vector v; // ok!
}


来源:https://stackoverflow.com/questions/9243629/c-set-and-vector-undeclared-despite-include-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!