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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:29:02+00:00 2026-06-12T21:29:02+00:00

I have a class template that has a boost matrix as a private member

  • 0

I have a class template that has a boost matrix as a private member variable. The matrix data type is determined by the class type when constructed. This class has a member function that is supposed to add a constant to the member matrix. The constant is consistent with the matrix data type. I am having trouble writing an overloaded operator that will return an updated member matrix for arbitrary constant values. Currently, another member function does this addition. My code is as follows;

/*content from main.cpp compiled with `g++ -o main main.cpp` */
#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

using namespace boost::numeric::ublas;
using namespace std;

template <typename T>
class MTool
{
private:
matrix<T> m_ME;

public: 
MTool(int N, int M) { m_ME.resize(M, N); } // constructor
~MTool() { m_ME.clear(); } // destructor
void PutMatValues(); // insert values into member matrix
void AddConst(const T &k) { m_ME+=k; } // add a constant to member matrix 
void PrintMat() { cout << "The ME " << endl << m_ME << endl; } // print the member matrix

// overloaded operator function
matrix<T> operator+ <> (const T& kvalue) { return (m_ME+kvalue); }  
};

template<typename T>
void MTool<T>::PutMatValues()
{
    for (unsigned row=0; row<m_ME.size1(); row++)
        for (unsigned col=0; col<m_ME.size2(); col++)   
            m_ME(row,col)=static_cast<T> (row*col);
} 

int main()
{
    MTool<double> mymat(5, 3); 
    double dk=123.67001;
    
    mymat.PutMatValues();   
    mymat.PrintMat();
    mymat.AddConst(dk);
    mymat.PrintMat();
     
    return 0; 
} 

Some of the compiler errors I get are

error: template-id ‘operator+<>’ in declaration of primary template

error: no match for ‘operator+=’ in ‘((MTool*)this)->MTool::m_ME += k’

I am rather new to C++ templates and classes and am sure there is something fundamental missing from my approach. Any suggestions would highly appreciated.

  • 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-12T21:29:03+00:00Added an answer on June 12, 2026 at 9:29 pm

    The first is just a syntax error, this member operator+ would be written as

    MTool<T> operator+ (const T& kvalue) const { ...
    

    although it’s somewhat unusual to see a binary plus operator as a member function: it’s almost always implemented as a non-member, so that you could write expressions c + M as well as M + c.

    The second error is simply pointing out that boost::numeric::ublas::matrix has no operator+= that takes a scalar argument. There is also no operator+ that can add a matrix and a scalar, so the expression m_ME+kvalue in your operator+ won’t compile either.

    Addition is only defined between matrices of equal shape. If you’re adding the scalar to every element of the matrix, you’re going to need to write that, something like:

    void AddConst(const T &k) {
        for(auto& element : m_ME.data())
               element += k;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to have a derived class (normal template) that has a variable of
I have a class myclass that has a private member param_map class some_class {
I have a template class with a variadic template member function that I am
I have a templated container class that looks something like this: template <class ItemType>
I have code that looks like this: template<class T> class list { public: class
I have a template statistics class that has range parameters. template <typename T> class
I have a class A that has a template parameter T. There are use
I have a template class that has lots of functions but is essentially a
I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I want to have a template class that looks something like what I have

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.