我对 c++ 很陌生,我需要一些关于这个错误的帮助,"数组类型 Item[1000] 不可分配" ?顺便说一下,我在XCode上,我知道我应该在Windows上。这是学校的家庭作业,所以如果你可以指导我,而不会放弃太多,那会很棒。哦,对不起如果我搞砸了什么,这是我的第一篇文章。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//global constant
const int MAX_SIZE = 1000;
//make an Item class
class Item
{
private:
string id;
int sold, remain;
public:
void set_id(string _id);
void set_sold(int _sold);
void set_remain(int _remain);
};
//all the methods setting id, sold, and remaining
void Item::set_id(string _id){
id = _id;
}
void Item::set_sold(int _sold){
sold = _sold;
}
void Item::set_remain(int _remain){
remain = _remain;
}
//code to read from file
ifstream inFile;
ofstream outFile;
//function that reads file to array
void read_In() **I think it's because this should not be a void**
{
//variables
int ct = MAX_SIZE;
Item a[MAX_SIZE];
//open the file
inFile.open( "inventory.txt" );
//while the file hasn't ended, read and place into array of Items
while( !inFile.eof() ){
string for_id = "";
int for_sold = 0;
int for_remain = 0;
for(int i = 0;i<ct;i++){
//store the next thing on the file to the array of Items
inFile >> for_id;
a[i].set_id(for_id);
inFile >> for_sold;
a[i].set_sold(for_sold);
inFile >> for_remain;
a[i].set_remain(for_remain);
}
}
inFile.close();
}
int main(){
Item sp[MAX_SIZE];
sp = read_In(); **Apparently the error is here!**
system("pause");
return 0;
}
首先正确关闭注释。一半的错误将被消除。其次,您正在使用,
void read_In(){
所以read_In返回 void.. 那么你不能接受它的价值,sp = read_In();
错了。