为什么我会收到错误"Array type Item[1000] is not assignable"?
Why am i getting the error "Array type Item[1000] is not assignable"?
我对 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();
错了。
相关文章:
- Why is UINT32_MAX + 1 = 0?
- C++错误:"error: int aaa::bbb is protected within this context"
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- Is !NaN not a NaN?
- Directx 11 - CompileFromFile() is not compiling
- Centos7 g++ "to_string is not in a member of std"
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- Where is std::hardware_destructive_interference_size?
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- What is unordered_set in C++
- 如何在C++中写入 1000 个文件时有效地缓冲
- 你如何理解"std: :forward is just syntactic sugar"?这是真的吗?
- C++ "error: use of overloaded operator '*' is ambiguous"似乎只有一场比赛
- C++ is calculating wrong
- Is it good to use SDL_PIXELFORMAT_UNKNOWN?
- reference_wrapper导致"incomplete type is not allowed"
- 为什么即使在定义之后仍存在"Identifier is undefined error "?
- 不断收到错误"member reference base type 'string [1000]' is not a structure or union"不知道如何解决?
- 为什么我会收到错误"Array type Item[1000] is not assignable"?