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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:28:26+00:00 2026-06-03T02:28:26+00:00

// vec2.h template<class v_float=float> class vec2 { public: v_float m[2]; }; template<class v_float> vec2<v_float>

  • 0
// vec2.h
template<class v_float=float>
class vec2
{
public:
    v_float m[2];
};

template<class v_float>
vec2<v_float> operator* (v_float & f, vec2<v_float> & v);

template<class v_float>
vec2<v_float> vec2<v_float>::operator* (v_float & f, vec2 & v)
{
    return vec2(v.m[0]*f, v.m[1]*f);
}

I want to overload the operator* for the above template class vec2, but the above code gave me the following error: vec2.cpp:68: error: ‘vec2 vec2::operator*(v_float&, vec2&)’ must take either zero or one argument

How to overload the operator*(float, vec2) for a template class? note that the number of parameters of operator* must be two.

  • 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-03T02:28:28+00:00Added an answer on June 3, 2026 at 2:28 am

    I think the problem is in this template member function:

    template<class v_float>
        vec2<v_float> vec2<v_float>::operator* (v_float & f, vec2 & v)
    {
        return vec2(v.m[0]*f, v.m[1]*f);
    }
    

    Since you’re defining operator * as a member function here, C++ automatically assumes that the receiver object is one of the operands. This means that if you want to define multiplication, you should only be defining a function that takes one argument, namely, the right-hand side of the multiplication (since the left-hand side will be the receiver object).

    I think you meant to have this as a free function, as shown here:

    template<class v_float>
        vec2<v_float> operator* (v_float & f, vec2<v_float> & v)
    {
        return vec2(v.m[0]*f, v.m[1]*f);
    }
    

    Hope this helps!

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

Sidebar

Related Questions

template <typename vec1, typename vec2> class fakevector { public: /* Do something */ };
Given: template<typename T> class A { B b; std::vector<T> vec1; std::vector<T> vec2; } I'd
Let's suppose I've got a 2D vector template class: template<typename T> class Vec2 {
Consider the following code: struct Vec2 : IEquatable<Vec2> { double X,Y; public bool Equals(Vec2
I'm writing a 2D Vector class whose declaration looks like: case class Vec2(x:Float, y:Float)
I have the following code in the Fragment Shader: precision lowp float; varying vec2
I have a property on my World class that looks like this: public Vec2
Here's the code: double angle( double *vec1 , double *vec2 ){ return acos( (
In JBox2d, there exists the following code for Vec2.equals() : @Override public boolean equals(Object
I wrote this code: vec3 col1 = texture2D(uDiffuseTexture, vec2(vTextureCoord.x, vTextureCoord.y + time)).rgb;\n + float

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.