-1

As I currently study Java in University but I am working on a C++ project. It is a text game in which I'm trying to create an empty vector of Item objects which I'm later going to add with the addItem(Item newItem) method of the Room object. The problem is that when in the constructor I try to set it so that every time a room is created, a new vector of Item objects is created with a capacity of the vector of 20, it gives me this error:

'Item::Item': no appropriate default constructor available

Here is the code:

Item.hpp:

#include <iostream>
#include <string>

#ifndef Item_HPP
#define Item_HPP

class Item {

    std::string description;

public:
    Item(std::string description);
    std::string getDescription();
    void setDescription(std::string description);
    void use();
};

#endif

Item.cpp:

#include "Item.hpp"

Item::Item(std::string description) {

    this->description = description;
}

std::string Item::getDescription() {

    return this->description;
}

void Item::setDescription(std::string description) {

    this->description = description;
}

void Item::use() {

    std::cout << "You are using item: " << description << std::endl;
}

Room.hpp:

#include <iostream>
#include <string>
#include <vector>
#include "Item.hpp"

#ifndef Room_HPP
#define Room_HPP


class Room {

    std::string description;
    static const int MAX_ITEMS = 20;
    std::vector <Item> items;
    int numberOfItems;


public:
    Room(std::string description);
    std::string getDescription();
    void addItem(Item newItem);
    std::vector<Item> getItems();

    Item getItem(std::string description);
};

#endif

Room.cpp:

#include "Room.hpp"

Room::Room(std::string description) {

    this->description = description;
    this->items = std::vector<Item>(20);
    this->numberOfItems = 0;
}

std::string Room::getDescription() {

    return this->description;
}

void Room::addItem(Item newItem) {

    if (numberOfItems < MAX_ITEMS) {
        this->items[numberOfItems] = newItem;
        numberOfItems++;
    }
}

std::vector<Item> Room::getItems() {

    return this->items;
}

Item Room::getItem(std::string description) {

    int i = 0;
    bool found = false;
    Item *target = NULL;
    while (!found && i < numberOfItems) {

        if (items[i].getDescription() == description) {

            target = &items[i];
            found = true;
        }
        ++i;
    }
    return *target;
}
  • Are you sure that all of that code is really necessary to demonstrate the problem you have? I am not. Provide a [mcve] (emphasis on _minimal_) as required here please. – πάντα ῥεῖ Dec 28 '20 at 23:05
  • @πάνταῥεῖ Hello as I am new to C++ I've been told by lecturers and teachers quite a lot that sometimes the problem in the code may not be in the place I think it might be, that's why I provided all the code which are part of the problem (as the program has more than 20 more classes), I am new to C++ and I am still learning the language, sorry for providing that much information! – ShadeyyWasTaken Dec 28 '20 at 23:11
  • You can simply reduce the code by commenting out unrelated stuff, and see if the error is still there. If not you've gone too far. After you have done that, the uncommented stuff is the essence you should post here. – πάντα ῥεῖ Dec 28 '20 at 23:14
  • Thank you I will keep that in mind for future question! – ShadeyyWasTaken Dec 28 '20 at 23:18
  • Also please [research a bit](https://www.google.com/search?q=site:stackoverflow.com+%22c%2B%2B%22+vector+error+no+default+constructor&rlz=1C1CHBF_deDE833DE833&sxsrf=ALeKk00la21l2UDc4ISt_l7QdB86HUd9jw:1609196871236&ei=R2XqX9z-DcuakwWlyoroAw&start=0&sa=N&ved=2ahUKEwicvoi_5fHtAhVLzaQKHSWlAj04ChDy0wN6BAgIEDY&biw=1600&bih=789) before asking another question here. – πάντα ῥεῖ Dec 28 '20 at 23:19
  • I've been searching for the same problem as mine but as I already said I am quite new to the language and as I am self-learning (As I study Java and try to transfer my knowledge to C++ when I learn something in Java), I actually checked the topics I've been provided with but still could not understand the problem (I've tried to find a solution to my problem for a few days before posting), that's why I asked this question. Thank you again for your responses I will keep all that in mind for future questions! – ShadeyyWasTaken Dec 28 '20 at 23:23
  • As you can see from my link, it's fairly easy to get the relevant information. You can also do like I did and restrct the search by using `site:stackoverflow.com`. Google is way better than the Stack Overflow inbuild search feature. – πάντα ῥεῖ Dec 28 '20 at 23:26

1 Answers1

0

You can firstly just reserve() buffer for 20 elements and later add elements via push_back().

Room::Room(std::string description) {

    this->description = description;
    this->items = std::vector<Item>();
    this->items.reserve(20);
    this->numberOfItems = 0;
}

void Room::addItem(Item newItem) {

    if (numberOfItems < MAX_ITEMS) {
        if (numberOfItems == static_cast<int>(this->items.size())) {
            this->items.push_back(newItem);
        } else if (numberOfItems < static_cast<int>(this->items.size())) {
            this->items[numberOfItems] = newItem;
        } else {
            // unsupported to create gap between items
        }
        numberOfItems++;
    }
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • 1
    More clearly: `items = std::vector(20);` does not only reserve space for 20 items, it also _creates_ those 20 items. – Mooing Duck Dec 28 '20 at 23:06
  • ...and since you haven't said otherwise, those items all need to be default constructed. – Jerry Coffin Dec 28 '20 at 23:10
  • is it possible to just initialise the vector with capacity of 20 in 1 line without the reserve() method without creating any items to it? And how can I create the array with the constructor I have created instead of the default one? Thank you for your earlier answer it helped me quite a lot! – ShadeyyWasTaken Dec 28 '20 at 23:12
  • @ShadeyyWasTaken • Change this `this->items = std::vector(20);` to this `this->items.reserve(20);` – Eljay Dec 28 '20 at 23:18