Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 273195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:25:14+00:00 2026-05-12T00:25:14+00:00

I wrote an abstraction class for a math object, and defined all of the

  • 0

I wrote an abstraction class for a math object, and defined all of the operators. While using it, I came across:

Fixed f1 = 5.0f - f3;

I have only two subtraction operators defined:

inline const Fixed operator - () const;
inline const Fixed operator - (float f) const;

I get what is wrong here – addition is swappable (1 + 2 == 2 + 1) while subtraction is not (same goes for multiplication and division).
I immediately wrote a function outside my class like this:

static inline const Fixed operator - (float f, const Fixed &fp);

But then I realized this cannot be done, because to do that I would have to touch the class’s privates, which results to using the keyword friend which I loath, as well as polluting the namespace with a ‘static’ unnecessary function.

Moving the function inside the class definition yields this error in gcc-4.3:

error: ‘static const Fixed Fixed::operator-(float, const Fixed&)’ must be either a non-static member function or a non-member function

Doing as GCC suggested, and making it a non-static function results the following error:

error: ‘const Fixed Fixed::operator-(float, const Fixed&)’ must take either zero or one argument

Why can’t I define the same operator inside the class definition? if there’s no way to do it, is there anyway else not using the friend keyword?

Same question goes for division, as it suffers from the same problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:25:15+00:00Added an answer on May 12, 2026 at 12:25 am

    If you need reassuring that friend functions can be OK:

    http://www.gotw.ca/gotw/084.htm

    Which operations need access to
    internal data we would otherwise have
    to grant via friendship? These should
    normally be members. (There are some
    rare exceptions such as operations
    needing conversions on their left-hand
    arguments and some like operator<<()
    whose signatures don’t allow the *this
    reference to be their first
    parameters; even these can normally be
    nonfriends implemented in terms of
    (possibly virtual) members, but
    sometimes doing that is merely an
    exercise in contortionism and they’re
    best and naturally expressed as
    friends.)

    You are in the “operations needing conversions on the left-hand arguments” camp. If you don’t want a friend, and assuming you have a non-explicit float constructor for Fixed, you can implement it as:

    static inline Fixed operator-(const Fixed &lhs, const Fixed &rhs) {
        return lhs.minus(rhs);
    }
    

    then implement minus as a public member function, that most users won’t bother with because they prefer the operator.

    I assume if you have operator-(float) then you have operator+(float), so if you don’t have the conversion operator, you could go with:

    static inline Fixed operator-(float lhs, const Fixed &rhs) {
        return (-rhs) + lhs;
        // return (-rhs) -(-lhs); if no operator+...
    }
    

    Or just Fixed(lhs) - rhs if you have an explicit float constructor. Those may or may not be as efficient as your friend implementation.

    Unfortunately the language is not going to bend over backwards to accommodate those who happen to loathe one of its keywords, so operators can’t be static member functions and get the effects of friendship that way ;-p

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wrote the following in PowersHell as a quick iTunes demonstration: $iTunes = New-Object -ComObject
I wrote a windows service using VB that read some legacy data from Visual
I wrote a simple tool to generate a DBUnit XML dataset using queries that
I wrote a game in c++ using SDL. The game currently updates a highscore
I wrote a simple class to manage business objects. class Manager { string[] GetNames();
I wrote a simple batch file as a PowerShell script, and I am getting
I wrote myself a little downloading application so that I could easily grab a
I wrote a component that displays a filename, a thumbnail and has a button
I wrote an application that currently runs against a local instance of MySql. I
I wrote a quick program in python to add a gtk GUI to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.