hamid
hamid

Reputation: 17

C++ assign values to a static array in a Class

// Class declaration

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Items
{
private:
    string description;
public:
    Items()
    {
        description = ""; }

    Items(string desc)
    {
        description = desc;}

    string getDescription() { return description; }

};
class InventoryItems {
public:
    static Items inventory[5];

};
// main function
int main()
{
    const int NUM = 3;
    InventoryItems inv;
    inv.inventory[0] = "Books";

    for (int i = 0; i < NUM; i++)
    {
        cout <<  inv.inventory[i].getDescription() << endl;
    }


    return 0;
}

I am getting below error:

invMain.cpp:31: error: no match for operator= in InventoryItems::inventory[0] = "Books" invMain.cpp:7: note: candidates are: Items& Items::operator=(const Items&)

Upvotes: 1

Views: 2156

Answers (2)

user4581301
user4581301

Reputation: 33952

There are a couple things wrong here:

static Items inventory[5];

static says that there is one and only one inventory for all InventoryItems. Unfortunately it doesn't allocate space for it with all compilers. OP could be seeing

undefined reference to `InventoryItems::inventory'

You can allocate storage with

class InventoryItems {
public:
    static Items inventory[5];

};

Items InventoryItems::inventory[5]; // needs to be defined outside the class

The other big problem is you're trying to stuff the square peg in the round hole and getting something along the lines of

error: no match for 'operator='

"Books" is a const char *, not an Items. const char * easily converts to a string because someone took the time to write functions that do the work. You will have to as well.

You can turn it into an Items and then assign it

inv.inventory[0] = Items("Books");

This creates a temporary Items and then after copying it destroys it. Bit expensive because you have a constructor and a destructor getting called just to copy one dang string.

You can also call do it like this:

InventoryItems::inventory[0] = Items("Books");

because all InventoryItems share the same inventory, you don't have to create an InventoryItems to get inventory.

If you don't want to create and destroy extra objects, you can write an assignment operator for Items that takes a string

Items & operator=(string desc)
{
    description = desc;
    return *this;
}

Now both

InventoryItems::inventory[0] = Items("Books");

and

InventoryItems::inventory[1] = "Pizza";

work.

You can also create a setter function in Items

void setDesc(string desc)
{
    description = desc;
}

Now you can for about the same cost as the operator=

InventoryItems::inventory[2].setDesc("Beer");

Which you chose it up to you. I personally like the setter in this case. It's more obvious what you are doing than the = and less expensive than the temporary variable.

Upvotes: 1

Dave the Sax
Dave the Sax

Reputation: 328

You haven't said what the error is. I compiled the same code in Visual Studio 2015 and got "binary '=': no operator found".

So the problem is that you haven't defined operator = for the Items class. You need this because Items isn't equivalent to string, even though at this moment it only contains a string.

Upvotes: 1

Related Questions