无法识别 Class 函数复制

Unidentifiable Class Function Copy

所以,我目前正在开发一个基于文本的角色扮演游戏,并且我 运行 在处理角色的库存时遇到了一个奇怪的问题。我收到以下错误:

qualified-id in declaration before '(' token

此错误位于我的 Inventory.cpp class 中的以下代码行:

void Inventory::addItem(Item *I){...}

当然,我意识到这还不够,所以这里是 Inventory.hInventory.cpp 的所有编码:

Inventory.h中:

 #ifndef INVENTORY_H
 #define INVENTORY_H

 #include "Item.h"
 #include <string>

 const int BACKPACK_SIZE = 16;

 class Inventory
 {
     public:
         Inventory();

         void addItem(Item *I);

         std::string getInventory();

         Item *backpack[BACKPACK_SIZE];
     protected:

     private:
 };

 #endif // INVENTORY_H

Inventory.cpp中:

#include "Inventory.h"


 Inventory::Inventory(){
     for(int i = 0; i < BACKPACK_SIZE; i++){
         backpack[i] = nullptr;
     }
 }

 std::string Inventory::getInventory(){
     std::string allItems = "";
     int counter = 1;
     for(int i = 0; i < BACKPACK_SIZE; i++){
          if(backpack[i] == nullptr){
             continue;
          }
          else{
              allItems += (counter + ".) " + backpack[i]->getName() + "\n");
              counter += 1;
          }

     return allItems;
 }

 void Inventory::addItem(Item *I){ //THIS LINE IS WHERE THE ERROR APPEARS
     for(int counter = 0; counter < BACKPACK_SIZE; counter++){
         if(backpack[counter] == nullptr){
             backpack[counter] == I;
             break;
         }
     }
 }

我已经完成了研究,但我根本无法弄清楚我究竟做错了什么。任何帮助深表感谢!旁注:如果人们不花时间评论我可以更改我的编码的其他事情,而是坚持这个特定问题,我们将不胜感激。谢谢!

这个问题可能会因为打字错误而被关闭,但我想说明一下@john 的评论,它可能会对您有所帮助。 (同时也回答你的问题)

 std::string Inventory::getInventory()
 {
     std::string allItems = "";
     int counter = 1;
     for(int i = 0; i < BACKPACK_SIZE; i++)
     {
          if(backpack[i] == nullptr)
          {
             continue;
          }
          else
          {
              allItems += (counter + ".) " + backpack[i]->getName() + "\n");
              counter += 1;
          }

     return allItems;
 }

您的代码已在此处重新格式化,以便左大括号和右大括号缩进到同一级别。

您可能会注意到,由于缺少大括号,您收到的错误更加明显。