Search code examples
c++classpointersgame-enginesoftware-design

Unidentifiable Class Function Copy


So, I am currently working on a text-based RPG, and I've run into an odd issue working on the character's inventory. I am getting the following error:

qualified-id in declaration before '(' token

This error is located at the following line of code in my Inventory.cpp class:

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

Of course, I realize that this isn't enough information to go by, so here is all the coding for both Inventory.h and Inventory.cpp:

In 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

In 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;
         }
     }
 }

I've done my research, yet I simply cannot figure what on earth I am doing wrong. Any help is much appreciated! Side note: it would be appreciated if people would not spend their time commenting on other things I could change about my coding, but rather stick to this specific issue. Thank you!


Solution

  • This question will likely be closed as a typo, but I'd like to illustrate a comment from @john that may help you. (While also answering your question)

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

    Your code has been reformatted here so that opening and closing braces are indented to the same level.

    You may notice that it's much more apparent that you are getting your error because of a missing brace.