invalid types 'unsigned int[unsigned int]' for array subscript

hi everyone!i'm working on a project and i'm getting two errors of the type mentioned in the title.
here's the part of the code that generates the problem:
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
34
35
36
37
38
39
40
41
42
#include "Instance.h"

//default constructor
Instance::Instance(unsigned features) {
	num_of_features = features;
	fileName = "-";
	category = true;

	keywords = new string[num_of_features];
	featureID = new unsigned[num_of_features];
	frequency = new unsigned[num_of_features];
	for (unsigned i = 0; i < num_of_features; i++)
	{
		keywords[i] = "-";
		featureID[i] = 0;
		frequency[i] = 0;
	}
}
//copy constructor
Instance::Instance(const Instance& original) {
	num_of_features = original.num_of_features;
	fileName = original.fileName;
	category = original.category;

	keywords = new string[num_of_features];
	featureID = new unsigned[num_of_features];
	frequency = new unsigned[num_of_features];
	for (unsigned i = 0; i < num_of_features; i++)
	{
		keywords[i] = original.keywords[i];
		featureID[i] = original.featureID[i];
		frequency[i] = original.frequency[i];
	}
}

//....

void Instance::setFeature(unsigned i, const string& feature, unsigned featureID, unsigned frequency) {
	keywords[i] = feature;
	featureID[i] = featureID;
	frequency[i] = frequency;
}


everything's declared in a header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Instance {
	friend ostream& operator<<(ostream& out, const Instance& inst);
	friend istream& operator>>(istream& in, Instance& inst);
public:
	Instance(unsigned features=0);
	Instance(const Instance& original);
	void setFeature(unsigned i, const string& feature, unsigned featureID, unsigned frequency);

private:
	string fileName;
	string* keywords;
	unsigned* featureID;
	unsigned* frequency;
	unsigned num_of_features;
	bool category;
};

the error appears in line 40 and 41 of the cpp file.
i've already searched similar topics but they didn't turn out to be very helpful..i would really appreciate any help!
thanks in advance
1
2
3
void Instance::setFeature(unsigned i, const string& feature, unsigned featureID, unsigned frequency) {
	keywords[i] = feature;
	featureID[i] = featureID;
It's simple, featureID is not an array but just 1 number.
However this->featureID that's an array.

Have fun with your memory leaks
i'm not sure i got this right..
first of all, if you're suggesting that i changed the code to this:
1
2
3
void Instance::setFeature(unsigned i, const string& feature, unsigned featureID, unsigned frequency) {
	keywords[i] = feature;
	featureID[i] = this->featureID;

i'm afraid it's not solving the problem.

secondly, featureID[i] is also just 1 variable that stores integers, no?
oops, sorry about the previous post!i got a bit hasty and i feel really stupid now.. :P
nevertheless, thanks a lot!!

about the memory leaks, a destructor alone won't do, right?
Last edited on
Topic archived. No new replies allowed.