Compilation error for reference returning operator []

Hi,

there is something I don’t understand, this code compiles:

struct X {

	float x,y,w;

	float &operator[](const int &i) {

		return x;

	}

};

__device__ void s() {

	X x;

	x.x = 3.0f;

	float h = 3.0f;

	x[1] = h;

}

this doesn’t

struct X {

	float x,y,w;

	float &operator[](const int &i) {

		switch(i) {

			case 0:

				return x;

		}

		return x;

	}

};

__device__ void s() {

	X x;

	x.x = 3.0f;

	float h = 3.0f;

	x[1] = h;

}

compilation fails with:

error: operand of "*" must be a pointer

Why does nvcc behave in this way?

I’m a bit fuzzy on the details, but does c really support -overloading on structs?

You could do what you do, in main, but inside a kernel, nvcc gets confused.

Just making wild guesses here.

You’d probably have better luck asking in the programming forums instead of the linux forum?

making the operator a device function will fix this