【问题标题】:Why am I getting the "Expression is not assignable" error?为什么我收到“表达式不可分配”错误?
【发布时间】:2014-12-06 04:00:16
【问题描述】:

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

我创建了两个返回的类方法,销售单位和剩余单位作为整数。

我想按从大到小对售出的单位进行排序,但我在 cmets 中解释时遇到了错误。

我做错了什么,是不是很明显?

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

}

【问题讨论】:

  • 你不能将函数调用的结果赋给另一个值。
  • 你建议我怎么做,看看是否有更小的单位出售?
  • 另外,您的 get_sold 方法为什么要传入一个值然后立即覆盖它?我认为你需要复习你的 C++ 并重新思考。
  • 只是为了安全起见,使用私人数据。由于您无法访问我正在尝试做的私人数据
  • 不,没有意义。它没有用。您的 get_remain 也有同样的问题。

标签: c++ xcode unit-testing


【解决方案1】:

在 C++ 中,int 是原始类型,而不是类,就像在 Java 中一样。如果你返回int,你只是把它作为一个常量返回,所以

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

不可能。您需要为您的班级提供适当的 getter 和 setter:

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模板和排序功能:

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

【讨论】:

  • 嗯.. 这是有道理的,但我设置我的 set_sold 的方式是在课堂上私人出售,而不是临时或其他任何东西......我可能不得不改变我的 set_sold
  • 应该没问题。在 C++ 中复制整数没有问题。
  • 不正确。在 C++ 中,函数可以返回一个可赋值的引用。当然,引用所指的变量必须有足够的生命周期才能在函数返回时仍然存在。做这样奇怪的事情是个好主意吗?没有。
  • 如果它引用了类的成员 - 那么也许。但是如果是局部变量,那么在函数返回控制权后,变量就会被销毁,对它的引用就会失效。
猜你喜欢
  • 2019-12-03
  • 2014-04-25
  • 2019-03-18
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-30
  • 2021-08-04
相关资源
最近更新 更多