xcode 为什么我收到“表达式不可分配”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27328166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting the "Expression is not assignable" error?
提问by Sam Perales
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?
}
}
}
回答by Dmitry
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
在 C++ 中int
是原始类型,而不是类,就像在 Java 中一样。如果你 return int
,你只是将它作为一个常量返回,所以
gL[i].get_sold(i) = something;
is not possible. You need to have proper getter and setter for your class:
不可能。您需要为您的班级配备适当的 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);
}
also, consider to use std::vector
template and sort function:
另外,考虑使用std::vector
模板和排序功能:
http://www.cplusplus.com/reference/algorithm/sort/
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];