2012-04-11 55 views
2

爲什麼下面的代碼會使編譯器崩潰?MSVC 2010模板映射類崩潰編譯器

#include <iostream> 
#include <string> 
#include <map> 

class test{ 
public: 
    template <typename T> 
    std::map<std::string, T> stuff; 
}; 

int main(int argc, char* argv[]) 
{ 
    test peanuts; 
    return 0; 
} 

在編譯器中是否存在錯誤?

回答

1

您試圖使用模板變量,您只能擁有類模板或函數模板。如果編譯器崩潰,那麼是一個錯誤,但它不是有效的C++。你可以做點像

class test{ 
    public: 
     template <typename T> 
     class Map { 
     public: 
      std::map<std::string, T> stuff; 
     }; 
    }; 

改爲。