subscript requires array or pointer type

I am getting this Error but I am not sure why. My function even returns a pointer to an array.
Error 2 error C2109: subscript requires array or pointer type c:\users\[user]\desktop\pedris khan\creepie crawlers\creepie crawlers\crawler.cpp 118 1 Creepie Crawlers



Function I am calling
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
  Molekule Molekule::getMolekuleList(){
	Molekule *molList = new Molekule[];
		Molekule a("Adenine", "A", "");
		Molekule c("Cytosine", "C", "");
		Molekule t("Adenine", "T", "");	
		Molekule g("Adenine", "G", "");		
		Molekule h("Adenine", "H", "");			
		Molekule i("Adenine", "I", "");	
		Molekule j("Adenine", "J", "");			
		Molekule p("Adenine", "P", "");			
		Molekule q("Adenine", "Q", "");				
		Molekule r("Adenine", "R", "");			
		Molekule s("Adenine", "S", "");		
		Molekule y("Adenine", "Y", "");			
		Molekule u("Adenine", "U", "");			
		Molekule x("Adenine", "X", "");
		
		molList[0] = a;
		molList[1] = c;
		molList[2] = t;
		molList[3] = g;
		molList[4] = h;
		molList[5] = i;
		molList[6] = j;
		molList[7] = p;
		molList[8] = q;
		molList[9] = r;
		molList[10] = s;
		molList[11] = y;
		molList[12] = u;
		molList[13] = x;

	return *molList;
}


Location of the Error
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
DNA Crawler::MutateDNA(){
	Molekule mol;
	DNA tmp;
	float mutationRate = readSettings("MutationRate");
	tmp = readDNAFile(*this);
	for (unsigned int i = 0; i < tmp.getChromosomes().size(); i++){

		for (unsigned int a = 0; a < tmp.getChromosomes()[i].getGenes().size(); a++){

			for (unsigned int b = 0; b < tmp.getChromosomes()[i].getGenes()[a].getPairs().size(); b++){
				for (int c = 0; c < 2; c++){
					srand(time(NULL));
					int x = (rand() % 100) + 1;
					if (x <= mutationRate){
						int x = rand() % 14;
						tmp.getChromosomes()[i].getGenes()[a].getPairs()[c] = mol.getMolekuleList[x];   //it gives me the error on this line
						
					}
				}
				}

			}
		}
	
	return tmp;
}
Molekule Molekule::getMolekuleList(){Returns a single value of type Molekule

mol.getMolekuleList[x];Trying to ally subscript operator to a single variable
In the mol.getMolekuleList[x], the subscript operator [] is applied to whatever the mol.getMolekuleList is. We only know about Molekule Molekule::getMolekuleList(), but in that case there should be a function call:
mol.getMolekuleList()[x]

In that case, as already noted, the subscript operator would be applied to a Molekule object (which is neither a pointer, array, or have subscript operator of its own).


PS. The line 2 in Molekule::getMolekuleList():
Molekule *molList = new Molekule[];
How many bytes does that allocate?
LOL I am so dumb.
I forgot the () and some other minor stuff.
Here is the new 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
DNA Crawler::MutateDNA(){
	Molekule mol;
	DNA tmp;
	float mutationRate = readSettings("MutationRate");
	tmp = readDNAFile(*this);
	for (unsigned int i = 0; i < tmp.getChromosomes().size(); i++){

		for (unsigned int a = 0; a < tmp.getChromosomes()[i].getGenes().size(); a++){

			for (unsigned int b = 0; b < tmp.getChromosomes()[i].getGenes()[a].getPairs().size(); b++){
				for (int c = 0; c < 2; c++){
					srand(time(NULL));
					int x = (rand() % 100) + 1;
					if (x <= mutationRate){
						int x = rand() % 14;
						tmp.getChromosomes()[i].getGenes()[a].getPairs()[b].getMolekules()[c] = mol.getMolekuleList()[x];
						
					}
				}
				}

			}
		}
	
	return tmp;
}

vector <Molekule> Molekule::getMolekuleList(){
	vector <Molekule> molList;
		Molekule a("Adenine", "A", "");
		Molekule c("Cytosine", "C", "");
		Molekule t("Adenine", "T", "");	
		Molekule g("Adenine", "G", "");		
		Molekule h("Adenine", "H", "");			
		Molekule i("Adenine", "I", "");	
		Molekule j("Adenine", "J", "");			
		Molekule p("Adenine", "P", "");			
		Molekule q("Adenine", "Q", "");				
		Molekule r("Adenine", "R", "");			
		Molekule s("Adenine", "S", "");		
		Molekule y("Adenine", "Y", "");			
		Molekule u("Adenine", "U", "");			
		Molekule x("Adenine", "X", "");
		
		molList.push_back(a);
		molList.push_back(c);
		molList.push_back(t);
		molList.push_back(g);
		molList.push_back(h);
		molList.push_back(i);
		molList.push_back(j);
		molList.push_back(p);
		molList.push_back(q);
		molList.push_back(r);
		molList.push_back(s);
		molList.push_back(y);
		molList.push_back(u);
		molList.push_back(x);

	return molList;
}

Thx
Last edited on
You could replace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
		molList.push_back(a);
		molList.push_back(c);
		molList.push_back(t);
		molList.push_back(g);
		molList.push_back(h);
		molList.push_back(i);
		molList.push_back(j);
		molList.push_back(p);
		molList.push_back(q);
		molList.push_back(r);
		molList.push_back(s);
		molList.push_back(y);
		molList.push_back(u);
		molList.push_back(x);
with vector <Molekule> molList = {c, t, g, h, i, j, p, q, r, s, y, u, x};

Or alternatively replace the entire body with
return { {"Adenine", "A", ""}, {"Cytosine", "C", ""}, ect...};
Topic archived. No new replies allowed.