Jump to content
  • Advertisement
Sign in to follow this  
_Sigma

[C++] Templated operator()

This topic is 3653 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to overload operator() as well as template it. Can this not be done? I keep getting:
error C2062: type 'double' unexpected
class foo
{
                //only these two functions are templated.
                template <typename T>
		inline T& operator() (unsigned  int row, int unsigned  col)
		{			
			[...]
		}
      
                template <typename T>
		inline T operator() (unsigned int row, unsigned int col) const
		{
			
			[...]
		}
};


foo bar;
cout << bar<double>(0,0); //this line


[Edited by - _Sigma on June 16, 2008 2:37:27 PM]

Share this post


Link to post
Share on other sites
Advertisement
Did you mean to write:

cout << (foo()).operator()<double>(0,0); // create a temporary foo object

or

cout << bar.operator()<double>(0, 0); // reference previously declared bar

Either way, file1 is not defined in that snippet.

[edit]

I had the syntax all wrong in my original posts.

[Edited by - fpsgamer on June 16, 2008 2:19:16 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by _Sigma
I am trying to overload operator() as well as template it. Can this not be done? I keep getting:

error C2062: type 'double' unexpected


*** Source Snippet Removed ***
*** Source Snippet Removed ***


Yeah, you need to explicitly state what you're doing:

bar.operator()<double>(0, 0);

// bar<double>() does not evaluate to operator()

Share this post


Link to post
Share on other sites
Quote:
Original post by fpsgamer
Did you mean to write:

cout << (foo()).operator()<double>(0,0); // create a temporary foo object

or

cout << bar.operator()<double>(0, 0); // reference previously declared bar

Either way, file1 is not defined in that snippet.

[edit]

I had the syntax all wrong in my original posts.

I meant #2. File1 is from my code...oops!

Thanks guys, it now works :)

Share this post


Link to post
Share on other sites
This seems like one of those situations where overloading () is a bad idea. Just use a named member function.

Share this post


Link to post
Share on other sites
Er, what does this class represent? It looks like you have some kind of 2-D array of things, which can be interpreted as some type T when they are accessed from the array. Have you considered:


typedef boost::multi_array<2, boost::any> foo;


or similar?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!