首页 > 解决方案 > “错误:'。'之前的预期主表达式 令牌”出现,我不知道为什么

问题描述

我对 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; } }

标签: c++compiler-errors

解决方案


没关系,我只需要这样做:

int main() {
    print print;
    int y = 1;
    int z = 1;
    while (y == 1) {
    cin >> z;
    string c = print.say(z);
    cout << c; } }

推荐阅读