4
我遇到了此問題標題中提到的錯誤。該代碼片段看起來是這樣的:錯誤:分配的只讀位置<unnamed> :: g_namesmap
namespace
{
struct myOptVar * g_optvar = 0;
//Variable that stores map of names to index
std::map<std::string, const size_t> g_namesmap;
};
void Optimizations::generate()
{
// free current optvar structure
free(g_optvar);
//clear our names map
g_namesmap.clear();
// create new optvar structure
const unsigned int size = g_items.size();
g_optvar = (struct myOptVar*)calloc(size, sizeof(struct myOptVar));
//copy our data into the optvar struct
size_t i=0;
for (OptParamMapConstIter cit=g_items.begin(); cit != g_items.end(); cit++, i++)
{
OptimizationParameter param((*cit).second);
g_namesmap[(*cit).first] = i; //error occurs here
...
g_namesmap聲明,並在未命名的命名空間中定義,它爲什麼認爲是「只讀」?