OY____ 2025-06-03 20:21 采纳率: 100%
浏览 18
已结题

[错误] expected unqualified-id before 'int'

这个报错该怎么解决

文件名                             行    列     描述
D:/C++/猜拳模拟.cpp    0    -1    In function 'int main()':
D:/C++/猜拳模拟.cpp    5    15    [错误] expected unqualified-id before 'int'
D:/C++/猜拳模拟.cpp    41    15    [说明] in expansion of macro 'stone'
D:/C++/猜拳模拟.cpp    6    15    [错误] expected unqualified-id before 'int'
D:/C++/猜拳模拟.cpp    42    15    [说明] in expansion of macro 'paper'
D:/C++/猜拳模拟.cpp    7    15    [错误] expected unqualified-id before 'int'
D:/C++/猜拳模拟.cpp    43    15    [说明] in expansion of macro 'sciss'
D:/C++/猜拳模拟.cpp    5    15    [错误] expected unqualified-id before 'int'
D:/C++/猜拳模拟.cpp    46    15    [说明] in expansion of macro 'stone'
D:/C++/猜拳模拟.cpp    6    15    [错误] expected unqualified-id before 'int'
D:/C++/猜拳模拟.cpp    47    15    [说明] in expansion of macro 'paper'
D:/C++/猜拳模拟.cpp    7    15    [错误] expected unqualified-id before 'int'
D:/C++/猜拳模拟.cpp    48    15    [说明] in expansion of macro 'sciss'
D:/C++/猜拳模拟.cpp    5    15    [错误] expected unqualified-id before 'int'
D:/C++/猜拳模拟.cpp    53    15    [说明] in expansion of macro 'stone'
D:/C++/猜拳模拟.cpp    6    15    [错误] expected unqualified-id before 'int'
D:/C++/猜拳模拟.cpp    54    15    [说明] in expansion of macro 'paper'
D:/C++/猜拳模拟.cpp    7    15    [错误] expected unqualified-id before 'int'
D:/C++/猜拳模拟.cpp    55    15    [说明] in expansion of macro 'sciss'
D:/C++/猜拳模拟.cpp    5    15    [错误] expected unqualified-id before 'int'
D:/C++/猜拳模拟.cpp    58    15    [说明] in expansion of macro 'stone'
D:/C++/猜拳模拟.cpp    6    15    [错误] expected unqualified-id before 'int'
D:/C++/猜拳模拟.cpp    59    15    [说明] in expansion of macro 'paper'
D:/C++/猜拳模拟.cpp    7    15    [错误] expected unqualified-id before 'int'
D:/C++/猜拳模拟.cpp    60    15    [说明] in expansion of macro 'sciss'
#include <bits/stdc++.h>
#define awin int(0)
#define bwin int(1)
#define nwin int(2)
#define stone int(0)
#define paper int(1)
#define sciss int(2)
using namespace std;

struct robot{
    int stone_=0, paper_=0, sciss_=0;
    int opt();
}r1, r2;

int robot::opt(){
    if(this->stone_>=this->paper_&&this->stone_>=this->sciss_) return stone;
    if(this->paper_>=this->stone_&&this->paper_>=this->sciss_) return paper;
    if(this->sciss_>=this->stone_&&this->sciss_>=this->paper_) return sciss;
}

int check(int a, int b){
    if(a==stone&&b==stone) return nwin;
    if(a==stone&&b==paper) return bwin;
    if(a==stone&&b==sciss) return awin;
    if(a==paper&&b==stone) return awin;
    if(a==paper&&b==paper) return nwin;
    if(a==paper&&b==sciss) return bwin;
    if(a==sciss&&b==stone) return bwin;
    if(a==sciss&&b==paper) return awin;
    if(a==sciss&&b==sciss) return nwin;
}

int main(){
    r2.paper_++;
    for(unsigned long long i;true;i++){
        int k=check(r1.opt(), r2.opt());
        char c=k==2?'n':k==0?'a':'b';
        printf("round %5llud:%c\n", i, c);
        if(k==0){
            switch(r1.opt()){
                case 0:r1.stone++;
                case 1:r1.paper++;
                case 2:r1.sciss++;
            }
            switch(r2.opt()){
                case 0:r2.stone--;
                case 1:r2.paper--;
                case 2:r2.sciss--;
            }
        }
        if(k==1){
            switch(r2.opt()){
                case 0:r2.stone++;
                case 1:r2.paper++;
                case 2:r2.sciss++;
            }
            switch(r1.opt()){
                case 0:r1.stone--;
                case 1:r1.paper--;
                case 2:r1.sciss--;
            }
        }
    }
    return 0;
}
  • 写回答

5条回答 默认 最新

  • 檀越@新空间 2025-06-03 20:22
    关注

    晚上好🌙🌙🌙
    本答案参考通义千问

    这个错误提示表明在代码中某些地方出现了语法问题,具体来说是 int 关键字被错误地使用了。以下是问题的分析和解决方案:


    问题分析

    1. 宏定义的问题

      • 在代码中,#define stone int(0) 等宏定义将 stone, paper, sciss 定义为 int(0), int(1), int(2)
      • 这里的 int 是 C++ 的关键字,不能直接作为宏定义的一部分,否则会导致语法错误。
    2. 宏展开问题

      • 当宏被展开时,例如 check(r1.opt(), r2.opt()),宏中的 int 被替换后会导致语法错误,因为 int 不是一个合法的标识符。
    3. 结构体成员变量的命名冲突

      • 结构体 robot 中有成员变量 stone_, paper_, sciss_,但宏定义中也有同名的符号(stone, paper, sciss),这会导致命名冲突。

    解决方案

    1. 修改宏定义

    将宏定义改为不包含 int 关键字的形式,例如:

    #define stone 0
    #define paper 1
    #define sciss 2
    

    2. 避免命名冲突

    为了避免宏定义与结构体成员变量的命名冲突,可以将宏定义改为其他名称,例如:

    #define STONE 0
    #define PAPER 1
    #define SCISS 2
    

    3. 修复代码逻辑

    • switch 语句中的 case 添加 break,否则会导致所有 case 都会被执行。
    • 修改 for 循环的初始化部分,确保 i 有一个初始值。

    修改后的代码

    以下是修复后的完整代码:

    #include <bits/stdc++.h>
    #define STONE 0
    #define PAPER 1
    #define SCISS 2
    
    using namespace std;
    
    struct robot {
        int stone_ = 0, paper_ = 0, sciss_ = 0;
        int opt();
    } r1, r2;
    
    int robot::opt() {
        if (this->stone_ >= this->paper_ && this->stone_ >= this->sciss_) return STONE;
        if (this->paper_ >= this->stone_ && this->paper_ >= this->sciss_) return PAPER;
        if (this->sciss_ >= this->stone_ && this->sciss_ >= this->paper_) return SCISS;
    }
    
    int check(int a, int b) {
        if (a == STONE && b == STONE) return 2; // 平局
        if (a == STONE && b == PAPER) return 1; // 对方胜
        if (a == STONE && b == SCISS) return 0; // 自己胜
        if (a == PAPER && b == STONE) return 0; // 自己胜
        if (a == PAPER && b == PAPER) return 2; // 平局
        if (a == PAPER && b == SCISS) return 1; // 对方胜
        if (a == SCISS && b == STONE) return 1; // 对方胜
        if (a == SCISS && b == PAPER) return 0; // 自己胜
        if (a == SCISS && b == SCISS) return 2; // 平局
    }
    
    int main() {
        r2.paper_++;
        unsigned long long i = 1; // 初始化 i
        while (true) { // 使用 while 循环替代无限循环
            int k = check(r1.opt(), r2.opt());
            char c = k == 2 ? 'n' : k == 0 ? 'a' : 'b';
            printf("round %5llu: %c\n", i, c);
    
            if (k == 0) { // 自己胜
                switch (r1.opt()) {
                    case STONE:
                        r1.stone_++;
                        break;
                    case PAPER:
                        r1.paper_++;
                        break;
                    case SCISS:
                        r1.sciss_++;
                        break;
                }
                switch (r2.opt()) {
                    case STONE:
                        r2.stone_--;
                        break;
                    case PAPER:
                        r2.paper_--;
                        break;
                    case SCISS:
                        r2.sciss_--;
                        break;
                }
            }
    
            if (k == 1) { // 对方胜
                switch (r2.opt()) {
                    case STONE:
                        r2.stone_++;
                        break;
                    case PAPER:
                        r2.paper_++;
                        break;
                    case SCISS:
                        r2.sciss_++;
                        break;
                }
                switch (r1.opt()) {
                    case STONE:
                        r1.stone_--;
                        break;
                    case PAPER:
                        r1.paper_--;
                        break;
                    case SCISS:
                        r1.sciss_--;
                        break;
                }
            }
    
            i++; // 增加轮数计数
        }
        return 0;
    }
    

    关键点总结

    1. 宏定义修正:避免使用 int 关键字,改为简单的数字或大写字母表示。
    2. 命名冲突处理:确保宏名称与结构体成员变量名称不冲突。
    3. 逻辑修复:为 switch 添加 break,确保每个分支独立执行;初始化 i 并使用 while 替代无限循环。

    通过以上修改,代码应该能够正常编译并运行。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 6月16日
  • 已采纳回答 6月8日
  • 创建了问题 6月3日