Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Differentiating between functions with same name from different header files

I’m currently writing a 2D vector class which contains a function to round x and y of the vector to the nearest int. to do this I thought I could use the <math.h> round() but the name of my Vector2 function is Vector2.round(), this causes an error because the computer thinks I’m giving too many arguments (1) because my Vector2.round() function isn’t meant to take any arguments, I though putting math::round() instead of round() might work but I guess the math library doesn’t use a namespace because it didn’t work, how can I tell the computer that I’m trying to access the <math.h> method as opposed to the one I wrote.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There Are Two Approaches:

  1. Using ::round() instead of math::round(). This Approach is however compiler dependent and as cited here works well only for Microsoft Or GNU Compiler.
  2. Using cmath library. Supports the same functions as math.h but its functions are present in std namespace allowing you to use std::round() as cited here
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading