Sign in to follow this  
Storyyeller

free functions don't work in a class

Recommended Posts

How come this doesn't work?
#pragma once

#include "vec2d.h"

struct PortalTransform
{
    Vec2d origin_offset;
    Vec2d dest_offset;
    Vec2d origin_normal;
    Vec2d dest_normal;

    double Angle() const {return Angle(dest_normal) - Angle(origin_normal);}
};


vec2d.h
#pragma once
#include <cmath>
//Other contents of header removed for brevity

inline double Angle(const Vec2d& arg)
{
    return atan2(arg.y, arg.x);
}


I assumed that PortalTransform::Angle() const would call the free function Angle(const Vec2d& arg), but apparently it throws a compiler error instead. Why?

Share this post


Link to post
Share on other sites
It's resolving the name "Angle" as "PortalTransform::Angle" and can't find an overload that takes one argument. It gives up before it goes out of the class scope. You have to tell it manually to look outside the class.


Put :: in front of the calls to the free-function.

i.e.

double Angle() const {return ::Angle(dest_normal) - ::Angle(origin_normal);}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this