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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:00:10+00:00 2026-05-26T21:00:10+00:00

What’s up guys, I am trying to overload the addition operator for my math

  • 0

What’s up guys,

I am trying to overload the addition operator for my math vector class.
My (seemingly logically correct) simplified code is:

template<typename T>
class Vector2
{
private:
    T       m_data[2];

    template<typename U>
    friend auto operator+(Vector2<T> a, Vector2<U> b) -> Vector2<decltype(a.m_data[0] + b.m_data[0])>
    {
        Vector2<decltype(a.m_data[0] + b.m_data[0])> ret(   a.m_data[0] + b.m_data[0],
                                                            a.m_data[1] + b.m_data[1]   );

        return ret;
    }

public:
    inline Vector2(T x, T y)
    {
        m_data[0] = x;
        m_data[1] = y;
    }
};

int main()
{
    Vector2<float>  v1(0.5f, 0.5f);
    Vector2<float>  v2(1, 2);

    v2 + v1; // Line 29

    return 0;
}

However, GCC 4.6.1 gave me this:

W:\projects\Awesome\BetterStuff\main.cpp||In function 'Vector2<decltype ((a.m_data[0] + b.m_data[0]))> operator+(Vector2<T>, Vector2<U>) [with U = float; T = float; decltype ((a.m_data[0] + b.m_data[0])) = float]':|
W:\projects\Awesome\BetterStuff\main.cpp|5|error: 'float Vector2<float>::m_data [2]' is private|
W:\projects\Awesome\BetterStuff\main.cpp|29|error: within this context|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|

And if I changed the second vector to an int vector, it would give me more (similar) errors.

The closest I came to figuring this thing out was finding this interesting page: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48884

But sadly, I couldn’t use it for solving my own problem.
I tried GCC 4.6.2 and 4.7.0 but my code didn’t compile either.

Changing “private” to “public” indeed solves my problem, but obviously my intention is not to expose m_data;

I just want to define an addition operator that its return type is determined by the template parameters, which from my understanding is, a compile time thing – for each instantiation of the template function, the compiler automatically figures out the return type based on the decltype() there. I mean, in which way main() is trying to access the contents of m_data for one of these vectors?

This whole thing is confusing me, any help would be greatly appreciated.

OK thanks

  • 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-26T21:00:11+00:00Added an answer on May 26, 2026 at 9:00 pm

    Well, GCC is right… The problem was not that Vector2(float) was trying to access Vector(int)’s private members, but that operator+ (which is only a friend of Vector2(float)) was trying to access Vector2(int)’s private members. So the updated code is:

    template<typename T>
    class Vector2
    {
        template<typename U>
        friend class Vector2;
    
    private:
        T       m_data[2];
    
        template<typename T1, typename U>
        friend auto operator+(Vector2<T1> a, Vector2<U> b) -> Vector2<decltype(a.m_data[0] + b.m_data[0])>;
    
    public:
        inline Vector2(T x, T y)
        {
            m_data[0] = x;
            m_data[1] = y;
        }
    
        inline Vector2<T>& operator=(const Vector2<T>& vec)
        {
            m_data[0] = vec.m_data[0];
            m_data[1] = vec.m_data[1];
        }
    };
    
    template<typename T, typename U>
    auto operator+(Vector2<T> a, Vector2<U> b) -> Vector2<decltype(a.m_data[0] + b.m_data[0])>
    {
        Vector2<decltype(T() + U())> ret(   a.m_data[0] + b.m_data[0],
                                            a.m_data[1] + b.m_data[1]   );
    
        return ret;
    }
    
    int main()
    {
        Vector2<float>  v1(0.5f, 0.5f);
        Vector2<int>    v2(1, 2);
    
        //Vector2<int>  a = v2 + v1; // Doesn't work
        Vector2<float>  b = v2 + v1; // Works
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.