Skip to main content
gamedev.net
🔒 Locked

operator() overloading - term does not evaluate to function

Started by Crispy 15 replies 1.9k views
Original Post
Crispy
Crispy
I was pretty sure the following code was correct until the compiler popped the abovementioned error every time I caleld it:
struct A {
  float* n;
  int d;
  float operator()(int x, int y) const;
  float& operator()(int x, int y);
  };

float A::operator()(int x, int y) const
{
  return n[y * d + x];
}

float& A::operator()(int x, int y)
{
  return n[y * d + x];
}


void main()
{
A a;

// ... initialize A''s memebers ...


a(2, 4) = 32;             //<- both produce the "term does not evaluate to function" error when compiling

float c = a(2, 4);
}
Can anyone spot a mistake?
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Ilici
Ilici
i dont think you can overload ")" and "(" only "[" and "]"
Crispy
Crispy
quote:
Original post by Ilici
i dont think you can overload ")" and "(" only "[" and "]"


I''m sorry, you''ll have to think twice on this one





"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Crispy
Crispy
quote:
Original post by Anonymous Poster
looks fine to me, should be int main() though, which compiler are you using?

Try a.operator()(2,4); see if that works


This is weird - ''operator()'' doesn''t show up in Code Completion, only ''operator[]'' does and VS .NET won''t allow me to overload either ''operator[][](int n)'' (which, AFAIK, should be wrong) or ''operator[](int x, int y)''.

Forcing what AP suggested gives a linker error.

PS: I only threw together this example to show how operator overloading is failing in my case - the fact that main() has the wrong syntax (alas, Borland allows you to use ''void main()'' as well), is irrelevant





"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Miserable
Miserable
quote:
Original post by Crispy
This is weird - ''operator()'' doesn''t show up in Code Completion, only ''operator[]'' does and VS .NET won''t allow me to overload either ''operator[][](int n)'' (which, AFAIK, should be wrong) or ''operator[](int x, int y)''.

Neither is valid. There is no such thing as operator[][] (the construct appears when operator[] returns something which has an operator[]), and operator[] can only take one parameter within the brackets.
Crispy
Crispy
Yes, I know ''[]'' can only take one parameter. In any case, how do go about overloading ''[]'' for a multidimensional array, eg the would-be ''[][]'' operator because I can''t see a way?







"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
davepermen
davepermen
just read the above answer..

[][] happens when [] returns an object wich has an OWN operator[]

class A1D {
A& operator[](uint index) { return a; }
A a;
}

class A2D {
A1D& operator[](uint index) { return a; }
A1D a;
}

A2D a;
A& a_item = a[1][2];

(and i know i forgot the ; at the end of the classes:D)




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
Crispy
Crispy
It''s an inherent "fault" in MSVC. Read more about it here (scroll down "Design issues" and read the following couple of paragraphs). Didn''t find an official explanation to it, though...

davepermen: oh... must''ve missed that.

AP: no, I don''t need the STL here, sorry - my case is much more trivial and doesn''t require the capabilities provided by STL.

Anyway, for the time being, I settled from the primitive Get(x, y) and Set(x, y, v) solution. It''s weird that .NET should have this discrepency, though...







"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Chris Hare
Chris Hare


class test
{
int** array;
public:
test()
{
array = new int*[3];
for (int i = 0; i < 3; i++)
array[i] = new int[2];
}

int* operator[](int dim)
{
return array[dim];
}
};



test[0][0] = 3;

first test.op[](0) is called, then builtin int*[].
There aren''t any restrictions on return types, so this could possibly be a solution.


Wizza Wuzza?
Crispy
Crispy
quote:
Original post by Miserable
See also http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.8


Yes, I already checked that link - it does everything the way I do, but doesn''t address the issue in MSVC. Somebody already compiled and ran this on mingw3.x...





"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.