Classification of anomaly-free 2HDMs with a gauged U(1)′ symmetry