compiling the code below with g++ main.cpp functions.cpp -o run
gives me the error error: 'vector' does not name a type
. Declaring namespace at the top of main.cpp
usually works across all .cpp
files for me.
main.cpp
using namespace std;
#include "functions.h"
main () {}
functions.h
#include <vector>
functions.cpp
#include "functions.h"
vector <int> x;
EDIT: I appreciate the fact all responders know what their talking about, but this normally works for me. Would the use of a makefile have any bearing on that? something else that I might be missing?
Yes but in this example functions.cpp
has not seen using namespace std
since you only wrote that in main.cpp
.
Don't add using namespace std
to functions.h
, use std::
to qualify types. Adding a using..
imposes an unnecessary burden on the user of your header.
You need to qualify the namespace:
#include "functions.h"
std::vector<int> x;
You have a using namespace std
in main.cpp
, and it cannot be seen by functions.cpp
. That is the root of the problem.
In general, you should avoid using namespace std
, specially in headers. And if you really must include it in main
, put it after all the headers.
You imported the std
namespace only in main.cpp
, not in functions.cpp
.
You have to qualify your use - std::vector
in the second file, or use the using
directive:
//functions.cpp
#include "functions.h"
std::vector <int> x; // preferred
or
//functions.cpp
#include "functions.h"
using namespace std;
vector <int> x;
or (bonus)
//functions.cpp
#include "functions.h"
using std::vector;
vector <int> x;
Declaring namespace at the top of main.cpp usually works across all .cpp files for me.
You have a really flawed compiler then. using
directives shouldn't influence translation units that don't have direct visibility over the directive.
You using namespace std
is local to main.cpp only. You need to use
std::vector<int> x;
in your source file functions.cpp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.