Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`does not name a type` error with `namespace std;` and files

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?

like image 687
Matt Munson Avatar asked Oct 21 '25 03:10

Matt Munson


1 Answers

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.

like image 128
cnicutar Avatar answered Oct 23 '25 16:10

cnicutar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!