[Solved] 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
    string name;
    int sold, remain;
    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?


Solution #1:

In C++ int is a primitive type, not a class, like in Java. If you return int, you just return it as a constant, so

gL[i].get_sold(i) = something;

is not possible. You need to have proper getter and setter for your class:

int Item::get_sold() {
    return sold;
void Item::set_sold(int s) {
    sold= s;


if(gL[i].get_sold() > gL[smallestPos].get_sold()) {

    temp = gL[i].get_sold();

also, consider to use std::vector template and sort function:

#include <algorithm>
#include <vector>

// Create comparsion function (or simple overload an < operator):
bool compareSold(Item i1, Item i2) {
    return i1.get_sold() < i2.get_sold();

// replace static array with std::vector<Item>
std::vector<Item> arrItems();

// You can init it with your static array, but better way would be
// to delete a static array and use this vector all the time.
arrItems.assign(gL, gL+ct); 

// Sort it
std::sort (arrItems.begin(), arrItens.end(), compareSold);

// If you need to convert std::vector to [] array, you can use &arrItems.front()
Item* i = &arrItems[0];
Respondent: Dmitry

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy