I don't understand the error

Hello,
I have a code but I can't compile and I get an error like this: (for line 5)
error C2109: subscript requires array or pointer type
1
2
3
4
5
6
7
8
void generate(BigInt amnt,string formule) {
	BigInt n0=0;
	vector <BigInt> Array(int(amnt));
	for (BigInt n=0;n<amnt;n++) {
		Array.at(int(n))=n0;
	}
...
}

Does someone know what's going wrong ?
The error message makes it sound like you are using operator[] on something that has not defined that operator, but I'm a bit confused because I don't see it being used in the code that you have posted.
Last edited on
I have not defined the [] operator for BigInt but it is just a vector with BigInt template so I think I shouldn't have to define that.
The error only came after I replaced a function int to_int() in my BigInt class to a function operator int() so the initial code which worked was
1
2
3
4
5
6
7
8
void generate(BigInt amnt,string formule) {
	BigInt n0=0;
	vector <BigInt> Array(amnt.to_int());
	for (BigInt n=0;n<amnt;n++) {
		Array.at(n.to_int())=n0;
	}
...
}
int(n) will try to cast BigInt to an integer, which is not working for you. Try n.int()
please post the error?
n.int() doesn't work.
This is the error.. :
error C2109: subscript requires array or pointer type
Last edited on
I replaced my code with this

1
2
3
4
void generate(BigInt amnt,string formule) {
	vector <BigInt> Array(int(amnt),0);
...
}

And I believe it works now. If anyone could tell me what was wrong feel free to tell me. I am always happy if I can learn something.
When I tried your code (with a lashed together, pretend BigInt) I got a different error, but at the same place.

main.cpp(46) : error C2228: left of '.at' must have class/struct/union


(For some reason, your compiler seems to be treating vector::at() as if it's vector::operator[] ??)

But if I tweak the code to use a C-style cast (int)amnt rather than a C++ function style cast int(amnt), then the problem goes away.

vector <BigInt> Array((int)amnt);

So it looks like you fell foul of the most vexing parse problem; the compiler thought that by (line 3)

vector <BigInt> Array(int(amnt));

you were declaring a function called Array which returned a vector of BigInts and takes a single int parameter (the brackets round amnt were just ignored.)

Andy

Most vexing parse
http://en.wikipedia.org/wiki/Most_vexing_parse
Last edited on
Thank you very much Andy!
Topic archived. No new replies allowed.