Search code examples
c++arraystypeerror

Gives an error adding string into array of string


Im trying to make a basic inventory management c++ program, what im trying to do is to add specific value (string) into array inventory (string too) here's the code:

#include <string>
class OurInventory{
    private:
        int size;
        std::string inventory;
    public: 
        void createArr(){
            std::cin >> size;
            std::string* inventory = new std::string[size];
            std::cout << "Created array with size of " << size << std::endl;
        }
        void addItem(){
            int userInputIndex;
            std::cin >> userInputIndex;
            if(userInputIndex >= size){
                std::cout << "Wrong value!";

            }
            else{
                std::string userInputData;
                std::cin >> userInputData;
                inventory[userInputIndex] = userInputData;
            }

        }
};
int main(){
    OurInventory obj;
    obj.createArr();
    obj.addItem();
}

So i tried declaring userInputData as an array of characters and i didnt worked too. I was excepting a prompt that asks me for specific index in the array and also a prompt that asked for what value to put in that index. It gave me this error:

error: cannot convert 'std::string' {aka 'std::__cxx11::basic_string<char>'} to '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' {aka 'char'} in assignment
   25 |                 inventory[userInputIndex] = userInputData;
      |                                       ^~~~~~~~~~~~~
      |                                       |
      |                                       std::string {aka std::__cxx11::basic_string<char>}

Solution

  • You redeclare inventory in your createArr() function, with a different type to boot. In C++ if you add a type before a parameter you (re)declare it for the current scope. The fix becomes:

    #include <string>
    class OurInventory{
        private:
            int size;
            std::string * inventory;
        public: 
            void createArr(){
                std::cin >> size;
                inventory = new std::string[size];
                std::cout << "Created array with size of " << size << std::endl;
            }
            void addItem(){
                int userInputIndex;
                std::cin >> userInputIndex;
                if(userInputIndex >= size){
                    std::cout << "Wrong value!";
    
                }
                else{
                    std::string userInputData;
                    std::cin >> userInputData;
                    inventory[userInputIndex] = userInputData;
                }
    
            }
    };
    int main(){
        OurInventory obj;
        obj.createArr();
        obj.addItem();
    }
    

    By removing the string* before the inventory in the createArr() function, I make sure it uses inventory the member variable.

    Some other recommendations:

    • You have no destructor of your class, so the memory you allocate to the inventory pointer will never be released and result in a memory leak.
    • Look into std::vector for creating an array of items. This has better memory management and has additional convenience functions added to it.