[C++] STL map 전역변수

wafe의 이미지

전역 변수로 map을 선언하고 초기화하는 코드가 컴파일이 안되더군요.

#include <string>
#include <map>

using namespace std;

map<int, string> intStrMap;
intStrMap[1] = "hello";
intStrMap[2] = "world";

int main()
{
  return 1;
}

Quote:

test.cpp:7: warning: ISO C++ forbids declaration of `intStrMap' with no type
test.cpp:7: error: conflicting types for `int intStrMap[1]'
test.cpp:6: error: previous declaration as `std::map<int, std::string,
std::less<int>, std::allocator<std::pair<const int, std::string> > >
intStrMap'
test.cpp:8: warning: ISO C++ forbids declaration of `intStrMap' with no type
test.cpp:8: error: conflicting types for `int intStrMap[2]'
test.cpp:7: error: previous declaration as `int intStrMap[1]'

맵에 내용을 넣는 부분을 함수 안으로 옮기면 잘 되네요.

#include <string>
#include <map>

using namespace std;

map<int, string> intStrMap;

int main()
{
  intStrMap[1] = "hello";
  intStrMap[2] = "world";
  return 1;
}

왜 이런 것인가요?