Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Operator Ambiguity
    text
    copied!<p>Forgive me, for I am fairly new to C++, but I am having some trouble regarding operator ambiguity. I think it is compiler-specific, for the code compiled on my desktop. However, it fails to compile on my laptop. I think I know what's going wrong, but I don't see an elegant way around it. Please let me know if I am making an obvious mistake. Anyhow, here's what I'm trying to do:</p> <p>I have made my own vector class called Vector4 which looks something like this:</p> <pre><code>class Vector4 { private: GLfloat vector[4]; ... } </code></pre> <p>Then I have these operators, which are causing the problem:</p> <pre><code>operator GLfloat* () { return vector; } operator const GLfloat* () const { return vector; } GLfloat&amp; operator [] (const size_t i) { return vector[i]; } const GLfloat&amp; operator [] (const size_t i) const { return vector[i]; } </code></pre> <p>I have the conversion operator so that I can pass an instance of my Vector4 class to glVertex3fv, and I have subscripting for obvious reasons. However, calls that involve subscripting the Vector4 become ambiguous to the compiler:</p> <pre><code>enum {x, y, z, w} Vector4 v(1.0, 2.0, 3.0, 4.0); glTranslatef(v[x], v[y], v[z]); </code></pre> <p>Here are the candidates:</p> <pre><code>candidate 1: const GLfloat&amp; Vector4:: operator[](size_t) const candidate 2: operator[](const GLfloat*, int) &lt;built-in&gt; </code></pre> <p>Why would it try to convert my Vector4 to a GLfloat* first when the subscript operator is already defined on Vector4? Is there a simple way around this that doesn't involve typecasting? Am I just making a silly mistake? Thanks for any help in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload