I built my People.cpp code and got the error "Unknown array size in delete" for line 6 and line 10. I never even used the delete function, and I don't think there should be an error because I included People.h in the file, which they still ignore and give me an error on. How do I fix it?
#ifndef SRC_PEOPLE_H_
#define SRC_PEOPLE_H_
#include "Human.h"
class People {
private:
int size;
int position;
Human humans[];
public:
People();
People(Human humans[], int size, int position);
void setHumans(Human humans[]);
void setSize(int size);
void setPosition(int position);
Human getHumans();
int getSize();
int getPosition();
Human search(std::string search);
Human insert(Human newHuman);
};
#endif /* SRC_PEOPLE_H_ */
And for humans[size], the size is for the user to enter the size of the array.
And for humans[size], the size is for the user to enter the size of the array.
No. In standard C++ the size of an array has to be set at compile time. In L11 of people.h the size of humans has to be set. This is usually done by specifying a maximum size and checking that input is less than that. Or better to use a std::vector where the size is determined at run-time.
Okay, but I want to put humans[] into an empty array, but when I type in humans[] or humans[] = [], it shows a syntax error in the code. How do I define it as an empty array?
Please don't start a new topic about the same subject.
Defining an empty array is like saying I want an egg carton that holds 0 eggs. Quite different is an egg carton that can hold 12 eggs, but has none at the moment.
Use a std::vector . it's size can vary automatically.
You don't. There is no such thing as an 'empty' array. An array has a size specified at compile time.
With array's you'd do something like:
1 2 3 4
constexpr size_t SIZE_ARRAY {20};
//...
Human humans[SIZE_ARRAY];
Then when populating humans from input, you'd check that the number of inputs don't exceed the specified size and keep the number of actually used elements.
Okay, but I found that the "Unknown array size in delete" is still persisting. I have decided to make the humans array not one of the private variables and comment out the getHumans and setHumans functions. Here is my code so far:
#ifndef SRC_PEOPLE_H_
#define SRC_PEOPLE_H_
#include "Human.h"
class People {
private:
int size;
int position;
public:
People();
People(int size, int position);
//void setHumans(Human humans[]);
void setSize(int size);
void setPosition(int position);
//Human getHumans();
int getSize();
int getPosition();
Human search(std::string search);
Human insert(Human newHuman);
Human humans[];
};
#endif /* SRC_PEOPLE_H_ */