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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:50:01+00:00 2026-05-27T22:50:01+00:00

I’m writing a vector template with a few specializations so that there are named

  • 0

I’m writing a vector template with a few specializations so that there are named accessors for the first few items of the vector (such x, y, z, w) depending on the size. KennyTM’s answer was a great help with this. I also added a form of Curiously recurring template pattern to be able to use the operators naturally. Here is the base class and a specialization with added functions (omitted most member implementations for brevity):

template<int Dim, typename V, typename ItemType = float>
class VectorImpl
{
public:
    typedef ItemType value_type;    
    VectorImpl() { elements.fill(0); }    
    VectorImpl(std::initializer_list<ItemType> init_list);

        V operator+(const V& other)
    {
        V result;
        for (int i = 0; i < Dim; ++i) 
            result.elements[i] = elements[i] + other.elements[i];

        return result;
    }

    QString toString();    
        // ... other members ...

protected:
    VectorImpl(const VectorImpl& other) = default;

protected:
    std::array<ItemType, Dim> elements;
};

template<int Dim, typename ItemType = float>
class Vector : public VectorImpl<Dim, Vector<Dim, ItemType>, ItemType>
{
    typedef VectorImpl<Dim, Vector<Dim, ItemType>, ItemType> ParentType;
public:
    Vector() : ParentType() {}
    Vector(const ParentType& other) : ParentType(other) {}
    Vector(std::initializer_list<ItemType> init_list) : ParentType(init_list) {}
};

template<typename ItemType>
class Vector<3, ItemType> : public VectorImpl<3, Vector<3, ItemType>, ItemType>
{
    typedef VectorImpl<3, Vector<3, ItemType>, ItemType> ParentType;
public:
    Vector() : ParentType() {}
    Vector(const ParentType& other) : ParentType(other) {}
    Vector(std::initializer_list<ItemType> init_list) : ParentType(init_list) {}
    ItemType x() const { return this->elements[0]; }
    ItemType y() const { return this->elements[1]; }
    ItemType z() const { return this->elements[2]; }
};

And I wanted to add qDebug()<< support, so I did this:

template<int Dim, typename ItemType>
QDebug operator<<(QDebug dbg, Vector<Dim, ItemType>& v)
{
    dbg.nospace() << v.toString();
    return dbg.space();
}

Now, the following code compiles and works:

Vector<3> v1 = { 3,4,5 };
qDebug() << v1;

This one does, too:

Vector<3> v1 = { 3,4,5 };
Vector<3> v2 = { 1,-1,1 };
qDebug() << v1;
auto v3 = v1 + v2;
qDebug() << v3;

But this one does not:

Vector<3> v1 = { 3,4,5 };
Vector<3> v2 = { 1,-1,1 };
qDebug() << (v1 + v2);

The compiler says:

error: no match for ‘operator<<‘ in ‘qDebug()() << v1.Vector<3>::.VectorImpl::operator+ [with int Dim = 3, V = Vector<3>, ItemType = float]((*(const Vector<3>*)(& v2)))’

What is going on? Why is the type of v1 + v2 different when assigning to a variable? What should I do to make this compile?

  • 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-27T22:50:01+00:00Added an answer on May 27, 2026 at 10:50 pm

    Give the output function a const reference, or rvalues (such as the temporary that is the result of your addition) won’t bind to it (and nor will actual constants, for that matter):

    QDebug operator<<(QDebug dbg, Vector<Dim, ItemType> const & v)
    //                                                  ^^^^^
    

    Also declare toString as const:

    QString toString() const;
    //                 ^^^^^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests

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.