What is "Unknown array size in delete?"

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?

Here is my People.cpp code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "Human.h"
#include "People.h"
using namespace std;


People::People() {
	 size = 0; position = 0; humans[size];
}

People::People(Human h[], int s, int p) {
	size=s;
	position=p;
	for (int i = 0; i < s; i++) {
		humans[i] = h[i];
	}

}

void People::setSize(int s) {
	size = s;
}

void People::setPosition(int p) {
	position = p;
}

int People::getSize() {
	return size;
}

int People::getPosition() {
	return position;
}
What is:

 
humans[size];


supposed to do?

Post the code for human.h and people.h
Here is my code for Human.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef HUMAN_H_
#define HUMAN_H_

//#include <iostream>
#include <string>

//using namespace std;

class Human {
	private:
		std::string name;
		int age;
		std::string phrase;
	public:
		Human(); //default constructor
		Human(std::string name, int age, std::string phrase); //constructor with three variables
		//setters
		void setName(std::string n);
		void setAge(int a);
		void setPhrase(std::string p);
		//getters
		std::string getName();
		int getAge();
		std::string getPhrase(); //print phrase


};



#endif /* HUMAN_H_ */


For People.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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?
Last edited on
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'd be better off using a vector rather than trying to resize an array.
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:

People.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "Human.h"
#include "People.h"
using namespace std;


People::People() {
	 size = 0; position = 0;
}

People::People(int s, int p) {
	size=s;
	position=p;
}

void People::setSize(int s) {
	size = s;
}

void People::setPosition(int p) {
	position = p;
}

int People::getSize() {
	return size;
}

int People::getPosition() {
	return position;
}

People.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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_ */ 
See my comments above re defining an array.
I already have removed all instances of the Human array from my People::People functions in my People.cpp code.
Nevermind, I fixed it now. I will get back to you when I run into another problem.
Topic archived. No new replies allowed.