c++ - 错误 : expected unqualified-id (C++)

标签 c++ class int void

我正在尝试用 C++ 创建一个基本的基于文​​本的冒险游戏。这是我的一门课。

#include "goblinFight.h"
#include <iostream>
#include <cmath>
#include <string>

using namespace std;


goblinFight::int fightGoblin()
{
cout << "A goblin attacks you! You: " << endl;
cout << "Raise your shield. (1)" << endl;
cout << "Take a step back. (2)" << endl;
cout << "Slash at it with your sword. (3)" << endl;
cin >> UI;
return UI;
}

goblinFight::void defendGoblin()
{
    switch(UI){
case 1: cout << "The goblin slashes at your with his " << goblinWeapon << ", but it bounces off your shield." << endl;
        playerShieldHealth -= 1;
        break;

case 2: cout << "The goblin steps forward..." << endl;
        switch(playerShieldHealth){
        case 0: playerShieldHealth += 2;
        break;
        case 1: playerShieldHealth += 1;
        break;}
        break;

case 3: srand(time(0));
        fightDice = rand() % 3;
        switch(fightDice){
        case 0: cout << "You strike the goblin! He bellows with rage!" << endl;
        goblinHealth -= 1;
        break;
        case 1: cout << "You strike the goblin! He screams in pain!" << endl;
        goblinHealth -= 1;
        break;
        case 2: cout << "You miss the goblin! He cackles at your ineptness." << endl;
        break;
        }
        break;
        }

我遇到的问题是它一直在第 9 行给我一个错误:错误:'int' 之前的预期非限定 ID。它在第 19 行给了我另一个错误,上面写着 - error: expected unqualified-id before 'void'。我无法弄清楚问题是什么,将不胜感激。

最佳答案

你有错误的返回类型和类名。

goblinFight::int fightGoblin() 应该是 int goblinFight::fightGoblin()

同上 goblinFight::void defendGoblin()

关于c++ - 错误 : expected unqualified-id (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22090498/

相关文章:

c++ - 为什么带有花括号初始化器列表的构造函数/虚拟析构函数不起作用?

c++ - 为什么我的代码无法编译? (完美转发和参数包)

C++ "operator+="和 "operator++"优先级问题

Java:使用 OOP 构建用户控制系统

将 "9632580147"转换为 int 时 Java 解析错误

c++ - Lua:避免 pcall 和 Lua 调用堆栈重载

python - 为什么我无法导入这个类?我不明白它是如何循环导入的

Java 反射,getMethod()

java - 在用户输入时验证数据类型

Haskell 函数似乎限制整数长度 - 我认为它使用 bignums?