为什么我得到“表达式不可分配"?错误? [英] Why am I getting the "Expression is not assignable" error?

查看:29
本文介绍了为什么我得到“表达式不可分配"?错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含私人名称、已售单位和剩余单位的课程.

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?
        }
    }

}

推荐答案

在 C++ 中 int 是原始类型,而不是 Java 中的类.如果你返回int,你只是将它作为一个常量返回,所以

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;

是不可能的.您需要为您的班级配备适当的 getter 和 setter:

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();
    gL[i].set_sold(gL[smallestPos].get_sold()); 
    gL[smallestPos].set_sold(temp);      
}

另外,考虑使用 std::vector 模板和排序函数:

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

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

#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];

这篇关于为什么我得到“表达式不可分配"?错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆