Ziv*_*ion -10 c++ dictionary cout class
我正在使用iostream和map。当我尝试设置函数时,它们会抛出错误。
我的代码:
#include "string"
#include "iostream"
#include "map"
using namespace std;
class myClass {
map<string, string> Map;
Map["Ziv"] = "Sion";
cout << Map["Ziv"];
};
Run Code Online (Sandbox Code Playgroud)
我的错误:
error: 'Map' does not name a type
error: 'cout' does not name a type
Run Code Online (Sandbox Code Playgroud)
为什么我不能使用iostreamand cout?
eer*_*ika 10
为什么我不能使用 iostream 和 cout?
因为一个类不能(直接)包含表达式语句。它只能包含成员声明。
表达式语句只能在函数内。这将是正确的,例如:
class main {
map<string, string> Map;
void example_function() {
Map["Ziv"] = "Sion";
cout << Map["Ziv"];
}
};
Run Code Online (Sandbox Code Playgroud)