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 8756501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:05:46+00:00 2026-06-13T14:05:46+00:00

I am trying to write struct Fraction that does basic operations at compile time.

  • 0

I am trying to write struct Fraction that does basic operations at compile time. Note that this does not serve any real purpose – I do this as an exercise only.

I started off with this:

namespace internal
{
    // Euclid algorithm
    template <int A, int B>
    struct gcd
    {
        static int const value = gcd<B, A % B>::value;
    };

    // Specialization to terminate recursion
    template <int A>
    struct gcd<A, 0>
    {
        static int const value = A;
    };
}

template <int Numerator, int Denominator>
struct Fraction
{
    // Numerator and denominator simplified
    static int const numerator = Numerator / internal::gcd<Numerator, Denominator>::value;
    static int const denominator = Denominator / internal::gcd<Numerator, Denominator>::value;

    // Add another fraction
    template <class Other> struct add
    {
        typedef Fraction<
            Numerator * Other::denominator + Other::numerator * Denominator,
            Denominator * Other::denominator
        > type;
    };
};

This compiles and works: Fraction<1,2>::add< Fraction<1,3> >::type will be Fraction<5,6>. Now I tried to add subtraction:

template <class Other>
struct sub
{
    typedef typename Fraction<Numerator, Denominator>::add<
        Fraction<-Other::numerator, Other::denominator>
    >::type type;
};

But I get a compiler error I do not understand:

Error: "typename Fraction<Numerator, Denominator>::add" uses "template<int Numerator, int Denominator> template <class Other> struct Fraction::add" which is not a type

Can someone explain to me what the compiler is saying, and why I am not allowed to do what I want? I am using g++ 4.4.6 by the way.

  • 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-06-13T14:05:47+00:00Added an answer on June 13, 2026 at 2:05 pm

    Use template keyword.

    template <class Other>
    struct sub
    {
        typedef typename Fraction<Numerator, Denominator>::template add<
            Fraction<-Other::numerator, -Other::denominator>
        >::type type;
    };
    

    http://liveworkspace.org/code/26f6314be690d14d1fc2df4755ad99f6

    Read this Where and why do I have to put the "template" and "typename" keywords? for better explanations.

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

Sidebar

Related Questions

This is the struct that I have and I'm trying to write the default
I'm trying to write some code that sets a property on a struct (important
I'm trying to write a POS-style application for a Sheevaplug that does the following:
Here is a struct I am trying to write: public struct AttackTraits { public
I m trying write code that after reset set up rrpmax as 3000. It
I am trying write a function that generates simulated data but if the simulated
Trying to write app for service technicians that will display open service calls within
Trying to write a couple of functions that will encrypt or decrypt a file
Trying to write a code at the moment that basically tests to see if
I'm trying to write code that uses a member typedef of a template argument,

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.