c++ - Why am I getting the "Expression is not assignable" error? -


i made class private name, units sold, , units remaining.

i made 2 class methods return, units sold , units remaining ints.

i want sort units sold greatest least, errors explain in comments.

what doing wrong, obvious?

#include <iostream> #include <string> #include <fstream> using namespace std;  const int max_size = 1000; const char file_name[14] = "inventory.txt";  //make 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 methods setting name, sold, , remaining, work though  int item::get_sold(int s) {     s = sold;      return s; } int item::get_remain(int r) {     r = remain;      return r; }  //greatest least units sold void sort_sold(item gl[], int ct) // ct global constant set 1000 {     //local variables     int smallestpos;     int temp;      //for every position in array     for(int i=0;i<ct;i++)     {         //find smallest element starting @ point         smallestpos = i;         for(int j=i+1;j<ct;j++)         {             if(gl[j].get_sold(j) < gl[smallestpos].get_sold(smallestpos))             {                 //found smaller one, remember , keep going                 smallestpos = j;             }         }         //see if found smaller gl[i].get_sold(i)         if(gl[i].get_sold(i) > gl[smallestpos].get_sold(smallestpos))         {             //we did find smaller one, swap 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?         }     }  } 

in c++ int primitive type, not class, in java. if return int, return constant, so

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

is not possible. need have proper getter , setter 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();     gl[i].set_sold(gl[smallestpos].get_sold());      gl[smallestpos].set_sold(temp);       } 

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

http://www.cplusplus.com/reference/algorithm/sort/

#include <algorithm> #include <vector>  // create comparsion function (or simple overload < operator): bool comparesold(item i1, item i2) {     return i1.get_sold() < i2.get_sold(); }      // replace static array std::vector<item> std::vector<item> arritems();  // can init static array, better way // delete static array , use vector time. arritems.assign(gl, gl+ct);   // sort std::sort (arritems.begin(), arritens.end(), comparesold);  // if need convert std::vector [] array, can use &arritems.front() item* = &arritems[0]; 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -