c++ - C++ 在类中使用 iostream
问题描述
我正在使用iostream
和map
。当我尝试设置功能时,它们会引发错误。
我的代码:
#include "string"
#include "iostream"
#include "map"
using namespace std;
class myClass {
map<string, string> Map;
Map["Ziv"] = "Sion";
cout << Map["Ziv"];
};
我的错误:
error: 'Map' does not name a type
error: 'cout' does not name a type
为什么我不能使用iostream
and cout
?
解决方案
为什么我不能使用 iostream 和 cout?
因为一个类不能(直接)包含表达式语句。它只能包含成员声明。
表达式语句只能在函数内。这将是正确的,例如:
class main {
map<string, string> Map;
void example_function() {
Map["Ziv"] = "Sion";
cout << Map["Ziv"];
}
};
推荐阅读
- java - 在eclipse中声明包
- raspberry-pi - 收到来电通知(通过 BLE)
- javascript - 通过 PHP 表单上传图片
- javascript - 将查询字符串添加到应用程序中的所有链接
- android - 关闭和 onCreate、Fragment$InstantiationException 和 IllegalStateException 时的片段对话框问题
- php - ldap 更新到 2.4.47 后 PHP 应用程序同步数据库无法正常工作
- javascript - 我的节点应用程序中的 NPM 模块是否共享依赖项?
- pandas - Pandas:选择多行或默认使用新 API
- python - 用pyqt5关闭窗口时如何关闭串口
- r - add_column 适用于矩阵