Why am I getting the “Expression is not assignable” error?

I made a class with private name, units sold, and units remaining.

I made two class methods that return, units sold and units remaining as ints.

I want to sort the units sold from greatest to least, but I get errors as I explain in the comments.

What am I doing wrong, is it something very obvious?

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

const int MAX_SIZE = 1000;
const char FILE_NAME[14] = "inventory.txt";

//make an Item class
class Item
{
private:
    string name;
    int sold, remain;
public:
    void set_name(string _name);
    void set_sold(int _sold);
    int get_sold(int);
    void set_remain(int _remain);
    int get_remain(int);
    void print();
};

//I erased all the methods setting name, sold, and remaining, they work though

int Item::get_sold(int s)
{
    s = sold;

    return s;
}
int Item::get_remain(int r)
{
    r = remain;

    return r;
}

//greatest to least units sold
void sort_sold(Item gL[], int ct) // ct is a global constant set to 1000
{
    //local variables
    int smallestPos;
    int temp;

    //for every position in the array
    for(int i=0;i<ct;i++)
    {
        //find the smallest element starting at that point
        smallestPos = i;
        for(int j=i+1;j<ct;j++)
        {
            if(gL[j].get_sold(j) < gL[smallestPos].get_sold(smallestPos))
            {
                //found a smaller one, remember and keep going
                smallestPos = j;
            }
        }
        //see if we found something smaller than gL[i].get_sold(i)
        if(gL[i].get_sold(i) > gL[smallestPos].get_sold(smallestPos))
        {
            //we did find a smaller one, so swap with gL[i].get_sold(i)
            temp = gL[i].get_sold(i);
            gL[i].get_sold(i) = gL[smallestPos].get_sold(smallestPos); //not assignable?
            gL[smallestPos].get_sold(smallestPos) = temp;              //not assignable?
        }
    }

}

Leave a Comment