I'm using Visual Studio (not sure if this is relevant here), I want to define a typedef for vector<int>::size_type in a header file.
This is my header:
#ifndef UTILS_H
#define UTILS_H
#include "pch.h"
#include <vector>
typedef int myint;
typedef vector<int>::size_type vi_sz;
#endif //UTILS_H
If I try to build it then I get the following errors:
...\utils.h(8): error C2143: syntax error: missing ';' before '<'
...\utils.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
...\utils.h(8): error C2039: 'size_type': is not a member of '`global namespace''
If I move typedef vector<int>::size_type vi_sz; to source file then everything is OK. Note that I don't need to do that with typedef int myint;
Is there a way to define that kind of typedef in header to avoid having to define it for every source file or is this bad practice in some way?
If I try to build it then I get the following errors:
Note that here:
#ifndef UTILS_H
#define UTILS_H
#include "pch.h"
#include <vector>
typedef int myint;
typedef vector<int>::size_type vi_sz;
#endif //UTILS_H
You don't have using namespace std; (as especially should be the case in header files) and yet you write vector<int>::size_type instead of std::vector<int>::size_type. Hence the name cannot be resolved.
If I move
typedef vector<int>::size_type vi_sz;to source file then everything is OK
This compiles when in the .cpp file as you probably have a using namespace std; prior to typedef vector<int>::size_type vi_sz; in there, and so the name is resolved. In short, just leave it in your header file like this: typedef std::vector<int>::size_type vi_sz;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With