1

So in my cpp file I'm trying to declare a map as follows:

map<string, vector<myStruct>> myMap;

At the top of my file I have written using namespace std and I also have #include <string> .

However I'm getting these weird errors:

error: ISO C++ forbids declaration of ‘map’ with no type

I don't know how to fix it. If I write #include <map> that just causes the compiler to freak out.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
user1782677
  • 1,963
  • 5
  • 26
  • 48
  • See the "Defined in header" note at the top [of this document](http://en.cppreference.com/w/cpp/container/map), then include that. And as mentioned in several answers, *don't* put `using namespace std;` in your header files. It's just a bad idea. – WhozCraig Apr 24 '13 at 00:49

4 Answers4

4

do you have #include <map>? rest looks valid, however you might need to add a space if your C++ standard is not C++11:

#include <map>
#include <vector>
#include <string>
using namespace std;

map<string, vector<myStruct> > myMap;
                           ^^^

even better not use namespace std:

#include <map>
#include <vector>
#include <string>

std::map<std::string, std::vector<myStruct> > myMap;
4pie0
  • 29,204
  • 9
  • 82
  • 118
0

You should also include <map>. std::map is introduced through this header.

Furthermore, using namespace std is considered a bad practice. You should either have a using statement or use the prefix the name with std:: to denote a fully-qualified identifier:

#include <map>
#include <string>
#include <vector>

std::map<std::string, std::vector<myStruct>> myMap;
Community
  • 1
  • 1
David G
  • 94,763
  • 41
  • 167
  • 253
0

You need to include map header file.

  #include <map>

Meanwhile, in case you are not using C++11, you need a space:

 map<string, vector<myStruct> > myMap;
                           //^^
taocp
  • 23,276
  • 10
  • 49
  • 62
0

Note, the lack of a using statement ;)

#include <vector>
#include <string>
#include <map>

#include <iostream>

typedef int myStruct;

std::map<std::string, std::vector<myStruct>> myMap;

int
main()
{
  std::vector<myStruct> testMe = { 1, 2, 3};
  myMap["myTest"] = testMe;
  std::cout << myMap.size() << std::endl;
  return(0);
}
jbphelps
  • 263
  • 3
  • 13
  • @tacp: Good point, depending on the compiler the >> may need to be > >. This compiled cleanly (and ran) using gcc 4.7.2. – jbphelps Apr 24 '13 at 00:51