C++代码为什么总提示"expected a declaration"错误?

//gua.cpp

#include "gua.h"  
#include <iostream>  
#include <string>  
#include <map>  
using namespace std;  
  
const string guaming[] = {"乾","兑","离","震","巽","坎","艮","坤"};  
const vector<string> yao[] = {{"阳","阳","阳"},{"阳","阳","阴"},{"阳","阴","阳"},{"阴","阴","阳"},{"阴","阳","阳"},{"阴","阳","阴"},{"阴","阴","阳"},{"阴","阴","阴"}};  
const string wuxing[] = {"金","金","火","木","木","水","土","土"};  
map<string, int> guaIndex; /*用于快速查找八卦名称的索引号*/
for (int i = 0; i < 8; i++) {  
    guaIndex[guaming[i]] = i;  
}
/*以下代码省略*/

为什么在for语句那里提示"expected a declaration."错误?

希望高手能帮忙找出根本原因,是在看不出哪里有语法错误!

阅读 1k
1 个回答

for 语句只能出现在函数里。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏