c++ - “错误:'。'之前的预期主表达式 令牌”出现,我不知道为什么
问题描述
我对 C++ 很陌生,我不确定为什么 "string c = print.say(z);" 如果我将它放在 main 或 main 的 while 循环中,则会产生错误。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class print {
public:
int x;
string say(int x) {
switch (x) {
case 1: return "text1";
case 2: return "text2";
case 3: return "text3";
default: return "default";} } };
int main() {
int y = 1;
int z = 1;
while (y == 1) {
cin >> z;
string c = print.say(z);
cout << c; } }
解决方案
没关系,我只需要这样做:
int main() {
print print;
int y = 1;
int z = 1;
while (y == 1) {
cin >> z;
string c = print.say(z);
cout << c; } }
推荐阅读
- c++ - ++i 与 i++ ?为什么 i++ 更快,而 ++i 应该更快?
- logic - 在棋盘上计算对角线的逻辑是什么?
- javascript - 对于大文件,使用 dataURI 创建 iframe 失败,有什么解决方法吗?
- oracle - 使用 Oracle 6i 和批处理数据库更改创建报告
- javascript - 确定数组中的位置
- python - Python计算两列的不同值
- java - docker 基础镜像更改导致 maven 无法正常工作
- python - 如何根据两个值检查两个变量
- javascript - Backgrid.SelectCell.extend 选项值必须是数字吗?
- flask - Authlib - 将授权 url 传递为 json