为什么我得到错误"数组类型项[1000]不可分配#34;?

时间:2014-12-05 01:17:01

标签: c++ xcode

我对c ++很陌生,我需要对此错误提供一些帮助, '数组类型项[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;
}

1 个答案:

答案 0 :(得分:2)

首先正确地发表评论。一半的错误将会消失。其次,你正在使用,

void read_In(){

所以read_In返回void ..然后你不能接受它的值,sp = read_In();是错误的。